我想要一个监视进程的"系统",并在以下情况下杀死所述进程:
该过程超出了一些内存要求
在某段时间内,该过程不响应来自"系统"的消息
我认为这个"系统"可能像监控过程一样简单吗?如何做到这一点的代码示例将是有用的.我当然不反对这个问题的完全不同的解决方案.
对于第一个要求,您可能希望研究使用ulimit
或调整系统上的内核OOM-killer设置.
监视守护进程也存在于此类事物中. 上帝是最近的一个例子.
我编写了一个作为cron作业运行的脚本,可以自定义以杀死问题进程:
#!/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; }
http://blog.endpoint.com/2012/08/automatically-kill-process-using-too.html