LInux OOM自定义响应

我正在运行Apache Web服务器,并希望稍微改善OOM情况的处理方式。

我对OOM评分很满意,并且已经在这方面做了一些定制,所以当一些不好的事情发生时,Linux正在消灭正确的stream程。 但是这还不够。

问题是,有时当OOM发生服务器变得超载,然后崩溃,必须重新启动。 我想处理,而不需要完全重新启动服务器。 所以我需要以某种方式在OOM杀手调用上“挂钩”一个脚本,这将杀死所有的apache(及其CGI)进程,从而释放内存并再次启动它(Apache)。

我知道这是可行的,因为如果OOM发生,而且我足够快地login到服务器并杀死Apache手册,那么一切都可以。

仅供参考,我现在运行了近百个这样的networking服务器,这就是为什么我正在寻找全自动解决scheme。

一个可能的解决scheme当然是使用一些能够parsing系统日志并以这种方式检测OOM的监视器 – 我已经有了类似的东西,通过电子邮件通知OOM杀人事件。 这个问题可以解决一些情况,但是如果OOM真的很糟糕,那么服务器太重了,我的脚本甚至不能启动(它由cron运行)。 可以通过使用inotify来观察系统日志,或者通过直接(例如通过fifo)将syslogpipe道传送给脚本来改进。

但是我仍然想知道 – 如何将脚本直接“钩”到OOM杀手? 所以我会把这样的东西放在一些/ etc / ..文件中:

oom_action="sh /path/to/my/script.sh kill" 

或者根本不可能这样做?

我使用CentOS 6,Apache 2.2和PHP作为FastCGI。

你为什么不监视Apache进程,并设置他们的oom_adj值为15,以确保他们将是第一个终止OOM? 以下是关于此设置的一些说明。

根据你的configuration,你可以修改Apache启动脚本或设置一个简单的cron任务来做到这一点。

您还可以定期观看命令dmesg | grep -i oom的输出 dmesg | grep -i oom 。 如果有任何线路,OOM杀手会杀死一个人,因为上次启动服务器。 然后可以用dmesg --clear清除缓冲区

我知道在野外有很多可怕的PHP应用程序,但是在Apache / FastCGI / PHP方面你不能做些什么吗? 阿帕奇不断OOMing是不是你应该经常遇到的东西。

尝试降低Apache进程和FastCGI处理程序的最大数量,并查看您的当前php.ini设置是否对于每个脚本的最大内存太高。

而且,对Apache使用ulimit是完全可能的,并限制进程可以使用的内存数量。 这可以帮助你之前,服务器几乎螺旋死亡。

OOM是非常不得已的,所有可以导致它的东西都应该被检查。

由于我没有find更好的解决scheme,我已经实现了看门狗读取内核系统日志消息(通过fifo):

/etc/rsyslog.d:

 (...) kern.err |/path/to/fifo 

并search他们的OOM杀手活动:

while read /path/to/fifo; do (..)

当OOM杀手大量产生(我真的只需要检查紧急情况),我杀了(然后启动)Apache。

我认为更好的是把你的进程放在cgroup内存子集中,当内存发生的时候使用release_agent来调用外部脚本

 notify_on_release contains a Boolean value, 1 or 0, that either enables or disables the execution of the release agent. If the notify_on_release is enabled, the kernel executes the contents of the release_agent file when a cgroup no longer contains any tasks (that is, the cgroup's tasks file contained some PIDs and those PIDs were removed, leaving the file empty). A path to the empty cgroup is provided as an argument to the release agent. release_agent (present in the root cgroup only) contains a command to be executed when a “notify on release” is triggered. Once a cgroup is emptied of all processes, and the notify_on_release flag is enabled, the kernel runs the command in the release_agent file and supplies it with a relative path (relative to the root cgroup) to the emptied cgroup as an argument. The release agent can be used, for example, to automatically remove empty cgroups 

使用cgroup你可以控制你处理多less资源可以使用,不要把你的服务器过载

 Using cgroup you can control the server resources