How to set OOM killer adjustments for daemons permanently?
Several modern dæmon supervision systems have a means for doing this. (Indeed, since there is a chain loading tool for the job, arguably they all have a means for doing this.)
- Upstart: Use
oom scorein the job file.
oom score -500
- systemd: Use the
OOMScoreAdjust=setting in the service unit. You can use service unit patch files to affect pre-packaged service units.
- daemontools family: Use the
oom-kill-protecttool from the nosh toolset in the
runprogram for the service.
If you are converting a system service unit, the
convert-systemd-unitstool will in fact convert the
OOMScoreAdjust=setting into such an invocation of
#!/bin/noshAs a bonus, you can make it parameterizable:
oom-kill-protect -- -500
oom-kill-protect -- fromenvand set the value of the parameter in the service's environment (presumed to be read from an envdir associated with the service, here manipulated with the nosh toolset's
rcctl set servicename oomprotect -500
- Jonathan de Boyne Pollard (2016).
oom-kill-protect. nosh toolset. Softwares.
- James Hunt and Clint Byrum (2014). "
oom score". Upstart Cookbook.
- Lennart Poettering (2013-10-07). "
systemd.exec. systemd manual pages. freedesktop.org.
- Jonathan de Boyne Pollard.
rcctl. nosh toolset. Softwares.
This is possible in Ubuntu using Upstart and the
oom score configuration option.
Linux has an "Out of Memory" killer facility. [...]
Normally the OOM killer regards all processes equally, this stanza advises the kernel to treat this job differently.
The "adjustment" value provided to this stanza may be an integer value from -999 (very unlikely to be killed by the OOM killer) up to 1000 (very likely to be killed by the OOM killer). [...]
# this application is a "resource hog" oom score 1000 expect daemon respawn exec /usr/bin/leaky-app
You could hack it into MySQL itself (e.g. OpenSSH's
sshd does this), yet that's a bit too hardcore and very dirty (problems with updates etc.)
You can do this in a wrapper or in the init script - the score should be inherited (and in a wrapper you would probably want to do
exec mysqld "$@" anyway).
cgroups - it will give you a bit more flexibility and it can be made permanent in the sense, that the appropriate settings can be applied automatically on service restart. See e.g. controlling priority of applications using cgroups for more info. To achieve the automatism you are looking for, you'll probably want to take a look at libcgroup, which contains a daemon that can handle changing cgroups of a running process on the fly according to a set of rules, or just use the
cgexec wrapper (from the same package).