Created
August 30, 2012 20:52
-
-
Save jonjensen/3540671 to your computer and use it in GitHub Desktop.
Script to automatically kill large processes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/perl | |
use strict; | |
use warnings; | |
use Proc::ProcessTable; | |
my $table = Proc::ProcessTable->new; | |
for my $process (@{$table->table}) { | |
# skip root processes | |
next if $process->uid == 0 or $process->gid == 0; | |
# skip anything other than Passenger application processes | |
#next unless $process->fname eq 'ruby' and $process->cmndline =~ /\bRails\b/; | |
# skip any using less than 1 GiB | |
next if $process->rss < 1_073_741_824; | |
# document the slaughter | |
(my $cmd = $process->cmndline) =~ s/\s+\z//; | |
print "Killing process: pid=", $process->pid, " uid=", $process->uid, " rss=", $process->rss, " fname=", $process->fname, " cmndline=", $cmd, "\n"; | |
# try first to terminate process politely | |
kill 15, $process->pid; | |
# wait a little, then kill ruthlessly if it's still around | |
sleep 5; | |
kill 9, $process->pid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice idea. Might be better than using ulimit to prevent memory hogs.
Regards
Racke