#!/bin/sh
#
# chkconfig: 2345 32 97
# description: Early OOM Killer. \
#	      A userspace service that will kill the largest process \
#	      (by VmRSS residential size) when free RAM drops below 10%.
# processname: earlyoom
# config: /etc/sysconfig/earlyoom
#

### BEGIN INIT INFO
# Provides:          earlyoom
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Early OOM Killer
# Description:       A userspace service that will kill the largest process
#                    (by VmRSS residential size) when free RAM drops below 10%.
### END INIT INFO

# Source function library.
WITHOUT_RC_COMPAT=1
. /etc/init.d/functions

# Source earlyoom configuration.
SourceIfNotEmpty /etc/sysconfig/earlyoom

LOGFILE=/var/log/earlyoom.log
LOCKFILE=/run/lock/subsys/earlyoom
RETVAL=0


start()
{
	start_daemon --lockfile "$LOCKFILE" --background --expect-user root -- earlyoom $EARLYOOM_ARGS 2> "$LOGFILE"
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --lockfile "$LOCKFILE" --expect-user root -- earlyoom
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	restart
	;;
  condrestart)
	if [ -f "$LOCKFILE" ]; then
		restart
	fi
	;;
  condstop)
	if [ -f "$LOCKFILE" ]; then
		stop
	fi
	;;
  status)
	status --lockfile "$LOCKFILE" --expect-user root -- earlyoom
	RETVAL=$?
	;;
  *)
	msg_usage "${0##*/} {start|stop|condstop|restart|condrestart|status}"
	RETVAL=1
esac

exit $RETVAL
