Skip to content

Instantly share code, notes, and snippets.

@ianchesal
Created September 1, 2012 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianchesal/3584525 to your computer and use it in GitHub Desktop.
Save ianchesal/3584525 to your computer and use it in GitHub Desktop.
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