4

In response to this.

Take a look to the questions about configuring the OOM killer. For example: https://unix.stackexchange.com/q/153585/21101

I am looking for what process would be killed first in the case of low memory.

Is there a way to see the OOM score for every process instead of each individually?

2 Answers 2

12

At its simplest, you could just do

cat /proc/*/oom_score

If you want more information - such as the PIDs and command string as well - then maybe a simple loop like

while read -r pid comm; do 
  printf '%d\t%d\t%s\n' "$pid" "$(cat /proc/$pid/oom_score)" "$comm"
done < <(ps -e -o pid= -o comm=)
1
  • The second item did the trick.
    – fixit7
    Jan 12, 2018 at 22:20
0

Here is a long one-liner which adds the OOM score of each process to the output of "ps":

export oom_prop="oom_score"
ps axfuww | perl -pe 's/^(USER\s+PID)/sprintf("%s %s", $1, uc($ENV{"oom_prop"}))/e' | perl -MFile::Slurp -pe 's~^(\S+\s+)(\d+)(\s+)~sprintf("%s%s%s%*s  ", $1, $2, $3, length($ENV{"oom_prop"}) - 1, read_file("/proc/$2/$ENV{\"oom_prop\"}", {err_mode => "quiet", chomp => 1}))~e' | less -S

The "ps" output looks like this:

USER         PID OOM_SCORE %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1         0  0.0  0.2 169612 13128 ?        Ss   Sep27   0:08 /sbin/init
root         275       508  0.0  2.2 186404 114428 ?       S<s  Sep27   0:06 /lib/systemd/systemd-journald
root         339         0  0.0  0.1  24260  6880 ?        Ss   Sep27   0:09 /lib/systemd/systemd-udevd
root         568       666  0.0  0.1 241940  8612 ?        Ssl  Sep27   0:16 /usr/lib/accountsservice/accounts-daemon
root         572       666  0.0  0.0   2540   768 ?        Ss   Sep27   0:05 /usr/sbin/acpid
...

You can control which OOM property to monitor by specifying "oom_score" or "oom_score_adj" in the export oom_prop initial command.

You also need the File::Slurp Perl module. It is a popular module and you should be able to install it either using the packaging system of your OS, or via CPAN.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .