Skip to content

Instantly share code, notes, and snippets.

@desyncr
Forked from jonjensen/kill-large-processes.pl
Last active June 21, 2016 11:51
Show Gist options
  • Save desyncr/8212800 to your computer and use it in GitHub Desktop.
Save desyncr/8212800 to your computer and use it in GitHub Desktop.
Small Perl script to monitor and kill processes that are using too much memory or CPU.
# Example inittab entry (/etc/inittab)
# this will create a daemonized process which init will watch and respawn as needed
1001::respawn:/usr/bin/perl /usr/local/bin/kill-large-processes.pl
#!/usr/bin/perl
use strict;
use warnings;
use Proc::ProcessTable;
use Getopt::Long;
my $table;
my %config = (
grace => 5,
sleep => 10,
cpu => 80.00,
memory => 1_073_741_824
);
GetOptions(
'grace=i' => \$config{grace},
'sleep=i' => \$config{sleep},
'cpu=f' => \$config{cpu},
'memory=f' => \$config{memory},
);
print "Initialized watchr\n";
while (1) {
$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
# TODO Move these checks to a config file - white list
#next unless $process->fname eq 'ruby' and $process->cmndline =~ /\bRails\b/;
# TODO Move these checks to a config file
my $cpu = $process->pctcpu; $cpu =~ s/\s//; # HACK
# skip any using less than 1 GiB
next unless $process->rss > $config{memory} ||
# skip any not abusing the cpu
($cpu =~ m/^\d+.\d+$/ && $cpu > $config{cpu});
# document the slaughter
(my $cmd = $process->cmndline) =~ s/\s+\z//;
print "Killing process: pid=", $process->pid, " uid=", $process->uid, " rss=", $process->rss, " cpu=", $cpu, " 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 $config{grace};
kill 9, $process->pid;
}
sleep $config{sleep};
}
print "Done\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment