Automatically kill process that consume too much memory or stall on linux Automatically kill process that consume too much memory or stall on linux linux linux

Automatically kill process that consume too much memory or stall on linux


For the first requirement, you might want to look into either using ulimit, or tweaking the kernel OOM-killer settings on your system.

Monitoring daemons exist for this sort of thing as well. God is a recent example.


I wrote a script that runs as a cron job and can be customized to kill problem processes:

#!/usr/local/bin/perluse 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;}

https://www.endpointdev.com/blog/2012/08/automatically-kill-process-using-too/


To limit memory usage of processes, check /etc/security/limits.conf