rfjakob / earlyoom

earlyoom - Early OOM Daemon for Linux
MIT License
2.96k stars 157 forks source link

Feature Request: Add option to protect a specific PID on the fly #287

Closed Holmes5 closed 1 year ago

Holmes5 commented 1 year ago

Sometimes I run a critical process that I really don't want to get killed, which is generally a Python process with the same name as other processes that I do want to kill when needed. It would be nice if there was a way (maybe through writing to a file or running a specialized command-line option) to inform a running earlyoom daemon that a process with a specific PID is mission critical and shouldn't be killed.

hakavlad commented 1 year ago

I suggest to consider a ready-made alternative.

nohang is able to take into account environment variables (and some other options) when choosing a victim.

Just add to a config:

@BADNESS_ADJ_RE_ENVIRON  -500  ///  AVOID_TO_KILL=TRUE

and nohang will avoid to kill processes with AVOID_TO_KILL=TRUE environment variable (-500 is like oom_score_adj, you can choose another value). Next, when the daemon is already running and you need to avoid killing newly started process, simply start the process with the appropriate variable:

$ AVOID_TO_KILL=TRUE ./some_script

This is one of many possible options: nohang supports several other ways to influence the choice of the victim (via process's cwd, exe realpath, cmdline, cgroup etc).

rfjakob commented 1 year ago

+1 for nohang.

The other option, that also works with earlyoom, is to set oom_score_adj for the process you don't want to get killed.