Browse Source

Protect some processes from the ravages of the oom-killer

Bob Mottram 11 years ago
parent
commit
b2adffc097
1 changed files with 45 additions and 0 deletions
  1. 45
    0
      beaglebone.txt

+ 45
- 0
beaglebone.txt View File

@@ -2025,6 +2025,51 @@ cp -r /home/myusername/.gnupg ~/
2025 2025
 chown -R root:root ~/.gnupg
2026 2026
 #+END_SRC
2027 2027
 
2028
+** Protect processes
2029
+Because the BBB has limited RAM some processes may occasionally be automatically killed if physical memory availability is getting too low.  The way in which processes are chosen to be sacrificed is not particularly intelligent, and so can result in vital systems being stopped.  To try to prevent that from ever happening the following script can be used, which should ensure that at a minimum ssh, email and mysql keep running.
2030
+
2031
+#+BEGIN_SRC: bash
2032
+emacs /usr/bin/protectprocesses
2033
+#+END_SRC
2034
+
2035
+Add the following:
2036
+
2037
+#+BEGIN_SRC: bash
2038
+#!/bin/bash
2039
+
2040
+declare -a protect=('/usr/sbin/sshd' '/usr/sbin/mysqld --basedir=/usr' '/bin/sh /usr/bin/mysqld_safe' '/usr/sbin/exim4')
2041
+
2042
+for p in "${protect[@]}"
2043
+do
2044
+  OOM_PROC_ID=$(ps aux | grep '$p' | grep -v grep | head -n 1 | awk -F ' ' '{print $2}')
2045
+  if [ ! -z "$OOM_PROC_ID" ]; then
2046
+    echo -1000 >/proc/$OOM_PROC_ID/oom_score_adj
2047
+    echo -17 >/proc/$OOM_PROC_ID/oom_adj
2048
+  fi
2049
+done
2050
+#+END_SRC
2051
+
2052
+Save and exit, then edit the cron jobs:
2053
+
2054
+#+BEGIN_SRC: bash
2055
+emacs /etc/crontab
2056
+#+END_SRC
2057
+
2058
+And add the line:
2059
+
2060
+#+BEGIN_SRC: bash
2061
+*/1            * *   *   *   root /usr/bin/timeout 30 /usr/bin/protectprocesses
2062
+#+END_SRC
2063
+
2064
+Then save and exit and restart cron.
2065
+
2066
+#+BEGIN_SRC: bash
2067
+chmod +x /usr/bin/protectprocesses
2068
+service cron restart
2069
+#+END_SRC
2070
+
2071
+Here cron is used so that if we stop one of the relevant processes and then restart it then its oom priority will be reassigned again
2072
+.
2028 2073
 ** Setting up a web site
2029 2074
 
2030 2075
 #+BEGIN_VERSE