aboutsummaryrefslogtreecommitdiff
path: root/system/earlyoom/rc.earlyoom
blob: 0e1cbb303df87b4a11600da5f00131b90030e272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/sh
set -eu

. /etc/default/earlyoom

do_start() {
	if [ -f /var/run/earlyoom.pid ];
	then
		if ps -p "$(cat /var/run/earlyoom.pid)" > /dev/null 2>&1
		then
			echo "earlyoom is already running."
			exit 0
		fi
	fi
	echo "Starting earlyoom..."
	# shellcheck disable=2086
	nohup /usr/sbin/earlyoom $EARLYOOM_ARGS > /var/log/earlyoom.log 2>&1 &
	echo "$!" > /var/run/earlyoom.pid
	exit 0
}

do_stop() {
	if [ -f /var/run/earlyoom.pid ];
	then
		if ps -p "$(cat /var/run/earlyoom.pid)" > /dev/null 2>&1
		then
			echo "Stopping earlyoom..."
			kill -15 "$(cat /var/run/earlyoom.pid)" > /dev/null 2>&1
			rm -f /var/run/earlyoom.pid
			exit 0
		fi
	fi
	echo "earlyoom is not running..."
}

do_force_stop() {
	if [ -f /var/run/earlyoom.pid ];
	then
		if ps -p "$(cat /var/run/earlyoom.pid)" > /dev/null 2>&1
		then
			echo "Killing earlyoom..."
			kill -9 "$(cat /var/run/earlyoom.pid)" > /dev/null 2>&1
			rm -f /var/run/earlyoom.pid
			exit 0
		fi
	fi
	echo "earlyoom appears to not be running."
	exit 0
}

do_restart() {
	do_stop
	do_start
}

do_status() {
	if [ -f /var/run/earlyoom.pid ];
	then
		if ps -p "$(cat /var/run/earlyoom.pid)" > /dev/null 2>&1
		then
			echo "earlyoom is running with pid $(cat /var/run/earlyoom.pid)."
			exit 0
		fi
	fi
	echo "earlyoom is not running."
}

do_help() {
	echo "USAGE: rc.earlyoom (start|stop|force-stop|restart|status)"
	exit 0
}

if [ -z "${1-}" ];
then
	do_help
fi

case $1 in
	start)
		do_start
		;;
	stop)
		do_stop
		;;
	restart)
		do_restart
		;;
	force-stop)
		do_force_stop
		;;
	status)
		do_status
		;;
	*)
		do_help
		;;
esac