Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I would like a "system" that monitors a process and would kill said process if:

  • the process exceeds some memory requirements
  • the process does not respond to a message from the "system" in some period of time

I assume this "system" could be something as simple as a monitoring process? A code example of how this could be done would be useful. I am of course not averse to a completely different solution to this problem.

like image 615
Stephen Cagle Avatar asked Oct 09 '08 15:10

Stephen Cagle


2 Answers

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.

like image 72
Avdi Avatar answered Sep 25 '22 17:09

Avdi


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

#!/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;
}

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

like image 43
Jon Jensen Avatar answered Sep 23 '22 17:09

Jon Jensen