#!/bin/sh
# Name: no_oom_killer.sh
# Author: Navaho Gunleg (http://navahogunleg.net/blog/)
# Version: 0.2
# Purpose: This script scans the process list for a couple of known
#	processes and shall attempt to set the oom_adj value of that
#	process to OOM_DISABLE (-17)
#
# Known issue:
#	- If your /etc/init.d/linuxsampler is calling this script,
#	  it shall confuse pgrep (2 processes with name "linuxsampler") and
#	  they shall all be disabled for OOM election. While not really a
#	  fatal problem, it can be worked around by renaming one of the two
#	  to LinuxSampler.
#	- Possibly more...
#
# Changes:
#	0.2:
#	- The script accepts 'stop' as argument to re-set the oom_adj 
#	  value to 0.
#	- Correctly handle the possibility of multiple processes running.
#	- Output changed slightly.
##############################################################################
# Binaries used:
CAT=/bin/cat
PGREP=/usr/bin/pgrep
# Configuration:
PROC="linuxsampler jackd"
OOM_DISABLE="-17" # value to put in oom_adj to disable election for this process
OOM_ENABLE="0" # value to put in oom_adj to re-enable election

# Default, disable stuff:
VALUE=$OOM_DISABLE
TEXT="Disabling";

# If 'stop' is passed, re-enable:
[ "x$1" = xstop ] && {
	VALUE=$OOM_ENABLE
	TEXT="Re-enabling";
}

# Now do stuff...
echo "$TEXT the OOM killer for processes [$PROC]:"
for P in $PROC;
do
	# There could be multiple processes running.
	PIDS=`$PGREP $P`
	for PID in $PIDS;
	do
		[ -n "$PID" ] && {
			echo -n "Process $P, PID $PID, oom_adj was `$CAT /proc/$PID/oom_adj`"
			echo $VALUE > /proc/$PID/oom_adj
			echo ", is now `$CAT /proc/$PID/oom_adj`."
		}
	done;
done;



