File: init.d

package info (click to toggle)
nagircbot 0.0.33-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 296 kB
  • sloc: cpp: 3,293; sh: 208; makefile: 90; ansic: 66
file content (320 lines) | stat: -rw-r--r-- 6,365 bytes parent folder | download
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/bin/sh

# Example init.d script with LSB support.
# http://refspecs.freestandards.org/LSB_2.1.0/LSB-generic/LSB-generic/iniscrptact.html
#
# Please read this init.d carefully and modify the sections to
# adjust it to the program you want to run.
#
# Copyright (c) 2007 Javier Fernandez-Sanguino <jfs@debian.org>
# Copyright (c) 2010 John Morrissey <jwm@horde.net>
#
# This is free software; you may redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2,
# or (at your option) any later version.
#
# This is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License with
# the Debian operating system, in /usr/share/common-licenses/GPL; if
# not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA

### BEGIN INIT INFO
# Provides:          nagircbot
# Required-Start:    $network $remote_fs
# Required-Stop:     $network $remote_fs
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: IRC bot that announces Nagios status
# Description:       An IRC bot that reads Nagios' status information and
#                    emits alerts to an IRC channel. It can filter alerts
#                    based on severity (CRITICAL, HARD, SOFT, and/or
#                    UNKNOWN) or by regular expression. It can connect to
#                    IRC servers protected by password or SSL, and can
#                    optionally set the topic to the current Nagios status.
### END INIT INFO

DAEMON=/usr/bin/nagircbot
NAME=nagircbot
DESC=nagircbot

PIDFILE=/var/run/$NAME.pid

if [ ! -x "$DAEMON" ]; then
	exit 0
fi

. /lib/lsb/init-functions

# How long to wait for the server to start, in seconds.
# If set, each time the server is started (on start or restart),
# wait to see if it starts successfully. If not set and the
# daemon takes a while to write its pid file, we might incorrectly
# state that the daemon did not start up.
# This value should always be at least 1.
STARTTIME=1

# How long to wait for the server to stop, in seconds.
# If set too low, some daemons may not stop gracefully
# and 'restart' will not work.
# This value should always be at least 1.
DIETIME=1

# Include defaults if available
if [ -f "/etc/default/$NAME" ]; then
	. "/etc/default/$NAME"
fi

# Check if pid's cmdline matches a given name.
running_pid() {
	local pid=$1
	shift

	local name=$1
	shift

	if [ -z "$pid" ]; then
		return 1
	fi
	if [ ! -d "/proc/$pid" ]; then
		return 1
	fi

	cmd=$(cat "/proc/$pid/cmdline" | tr "\000" "\n" | head -n1 | cut -d: -f1)
	# Is this the expected server?
	if [ "$cmd" != "$name" ]; then
		return 1
	fi

	return 0
}

running() {
	local pid

	# No pidfile, probably no daemon present.
	if [ ! -f "$PIDFILE" ]; then
		return 1
	fi
	pid=$(cat "$PIDFILE")
	if ! running_pid "$pid" "$DAEMON"; then
		return 1
	fi
	return 0
}

start_server() {
	start_daemon -p "$PIDFILE" "$DAEMON" -P "$PIDFILE" \
		-z "$USER" -Xf "$STATUSFILE" \
		-s "$SERVER" -n "$NICK" -c "$CHANNEL" $OPTIONS
}

stop_server() {
	killproc -p "$PIDFILE" "$DAEMON"
}

reload_server() {
	if [ ! -f "$PIDFILE" ]; then
		return 1
	fi
	pid=$(pidofproc "$PIDFILE")
	kill -HUP $pid
	return
}

# Force the process to die. SIGTERM it at first,
# then SIGKILL after $DIETIME.
force_stop() {
	if [ ! -e "$PIDFILE" ]; then
		return 0
	fi
	if ! running; then
		rm -f "$PIDFILE"
		return 0
	fi

	kill -TERM "$pid"
	if ! running; then
		rm -f "$PIDFILE"
		return
	fi

	sleep "${DIETIME:-1}"

	kill -KILL $pid
	sleep "${DIETIME:-1}"
	if ! running; then
		rm -f "$PIDFILE"
		return
	fi

	echo "Cannot kill $NAME (pid=$pid)!"
	return 1
}

case "$1" in
start)
	case $ENABLE in
	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee])
		exit 0
		;;
	esac

	log_daemon_msg "Starting $DESC " "$NAME"

	if running; then
		log_progress_msg "already running"
		log_end_msg 0
		exit
	fi

	if ! start_server; then
		log_end_msg 1
		exit
	fi

	if ! running; then
		sleep "${STARTTIME:-1}"
	fi
	if ! running; then
		log_end_msg 7
		exit
	fi

	log_end_msg 0
	exit
	;;

stop)
	log_daemon_msg "Stopping $DESC" "$NAME"

	if ! running; then
		log_progress_msg "not running"
		log_end_msg 0
		exit
	fi

	stop_server
	log_end_msg $?
	exit
	;;

force-stop)
	# First, try to gracefully stop the program.
	"$0" stop
	if ! running; then
		exit 0
	fi

	# It's still running; try to kill it more forcefully.
	log_daemon_msg "Stopping (force) $DESC" "$NAME"
	force_stop
	log_end_msg $?
	exit
	;;

restart|force-reload)
	case $ENABLE in
	[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee])
		exit 0
		;;
	esac

	log_daemon_msg "Restarting $DESC" "$NAME"

	if ! running; then
		log_end_msg 0
		exit
	fi

	stop_server
	status=$?
	if [ $status -ne 0 ]; then
		if [ "$1" = 'force-reload' ]; then
			log_end_msg 0
		else
			log_end_msg $status
		fi
		exit
	fi

	# Some servers take time to stop.
	sleep "${DIETIME:-1}"

	start_server
	status=$?
	if [ $status -ne 0 ]; then
		if [ "$1" = "force-reload" ]; then
			log_end_msg 0
		else
			log_end_msg $status
		fi
		exit
	fi

	sleep "${STARTTIME:-1}"

	if ! running; then
		if [ "$1" = "force-reload" ]; then
			log_end_msg 0
		else
			log_end_msg 7
		fi
		exit
	fi

	log_end_msg 0
	exit
	;;

status)
	status_of_proc -p "$PIDFILE" "$DAEMON" "$NAME"
	exit
	;;

# Use this if the daemon cannot reload...
reload)
	log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
	log_warning_msg "cannot re-read the config file (use restart)."
	exit 3
	;;
# ... and this if it can.
#reload)
	# If the daemon can reload its config files on the fly
	# (for example, by sending it SIGHUP):
	#log_daemon_msg "Reloading $DESC configuration" "$NAME"
	#if ! running; then
	#	log_progress_msg "not running"
	#	log_end_msg 7
	#	exit
	#fi
	#
	#reload_server
	#if ! running; then
	#	log_progress_msg "died on reload"
	#	log_end_msg 1
	#	exit
	#fi
	#exit 0
	#;;

	# If the daemon responds to changes in its config file
	# automatically, make this a do-nothing entry:
	#exit 0
	#;;

*)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
	exit 3
	;;
esac

exit 0