/
sparked-init
executable file
·105 lines (89 loc) · 2.09 KB
/
sparked-init
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
#!/bin/sh
# Generic initscript for Sparked applications. This script is meant to
# be source'd from a wrapper script in /etc/init.d/. It expects a
# single variable to be set, $APPLICATION, which is the name of the
# Sparked app. See doc/initscript in the Sparked distribution for an
# example of how to do this.
#
# Author: Arjan Scherpenisse <arjan@scherpenisse.net>
if [ "$APPLICATION" = "" ]; then
echo "The variable APPLICATION needs to be set."
exit 1
fi
RUN_UID=nobody
RUN_GID=nogroup
PIDFILE=/var/run/$APPLICATION.pid
LOGFILE=/var/log/$APPLICATION.log
OPTIONS=""
REACTOR=""
if [ -f /etc/default/$APPLICATION ]; then
. /etc/default/$APPLICATION
fi
touch $LOGFILE && chown $RUN_UID:$RUN_GID $LOGFILE
DAEMON=/usr/bin/twistd
DAEMON_OPTS="--pidfile $PIDFILE --uid $RUN_UID --gid $RUN_GID sparked -s $APPLICATION $OPTIONS"
if [ "$REACTOR" != "" ]; then
DAEMON_OPTS="-r $REACTOR $DAEMON_OPTS"
fi
DB=/var/lib/$APPLICATION
mkdir -p $DB && chown -R $RUN_UID:$RUN_GID $DB
if [ ! -x $DAEMON ]; then
echo "ERROR: Can't execute $DAEMON."
exit 1
fi
start_service() {
echo -n " * Starting $APPLICATION... "
start-stop-daemon -Sq -p $PIDFILE -x $DAEMON -- $DAEMON_OPTS
e=$?
if [ $e -eq 1 ]; then
echo "already running"
return
fi
if [ $e -eq 255 ]; then
echo "couldn't start :("
return
fi
echo "done"
}
stop_service() {
echo -n " * Stopping $APPLICATION... "
start-stop-daemon -Kq -R 10 -p $PIDFILE
e=$?
if [ $e -eq 1 ]; then
echo "not running"
return
fi
rm -f $PIDFILE
echo "done"
}
reload_service() {
echo -n " * Reloading $APPLICATION... "
start-stop-daemon -Kq -p $PIDFILE -s USR1 # rotate logs
start-stop-daemon -Kq -p $PIDFILE -s USR2 # loadService() call in sparked Application
e=$?
if [ $e -eq 1 ]; then
echo "not running"
return
fi
echo "done"
}
case "$1" in
start)
start_service
;;
stop)
stop_service
;;
restart)
stop_service
start_service
;;
force-reload|reload)
reload_service
;;
*)
echo "Usage: /etc/init.d/$APPLICATION {start|stop|restart}" >&2
exit 1
;;
esac
exit 0