Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Running some Linux servers with single or just a few vital system service daemons, I would like to adjust the OOM killer for those daemonized processes in case something odd happens. For example, today some Ubuntu server running MySQL got a killed MySQL daemon because tons of apt-checker processes were consuming all memory and the kernel thought it was a good idea to kill MySQL.

I know I can adjust the score using the /proc/$(pidof mysqld)/oom_score_adj file to give the kernel some clue I don't prefer MySQL to be killed, yet that doesn't survive a restart of the service. Should I edit init/upstart scripts from the package to include these adjustments? I don't think that's a very elegant solution as I would make adjustments to files belonging to a package. Would it be possible to hook into upstart/init scripts in general and conditionally adjust it? Or would you suggest running an indefinite script like while true{ adjust_oom(); sleep 60;}?

share|improve this question
    
Interesting that there is the possibility to adjust that. I guess that there is nothing better than your infinite loop to accomplish the job. The OOM-killer is buried deep inside the kernel and has a very obscure algorithm. –  Nils Dec 19 '12 at 22:35

2 Answers 2

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).

Use 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).

share|improve this answer
up vote 4 down vote accepted

This is possible in Ubuntu using Upstart and the oom score configuration option.

oom score

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). [...]

Example:

# this application is a "resource hog"
oom score 1000

expect daemon
respawn
exec /usr/bin/leaky-app
share|improve this answer
    
I'm still surprised as to why I didn't find this earlier on. Today I stumbled upon it by accident. :) –  gertvdijk Jan 21 '13 at 20:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.