Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Open File Descriptors per Process
#!/usr/bin/env perl
# Works on any system that supports a 'ps aux' call and the lsof
# command. Counts the number of open file descriptors for each
# process in the 'ps aux' output. Handy if you're trying to see
# which process is hanging on to the most FDs.
use strict;
printf("%-10s %-10s %s\n", 'PID', 'OPEN FDS', 'COMMAND');
my @ps_output = split(/\n/, `ps aux`);
shift(@ps_output);
foreach my $line (@ps_output) {
# 1 is the PID, 10 is the command description
my ($pid, $command) = (split(/\s+/, $line, 11))[1,10];
my $open_fds = `lsof -p $pid | wc -l`;
chomp($open_fds);
printf("%-10s %-10s %s\n", $pid, $open_fds, $command);
}
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.