-
Notifications
You must be signed in to change notification settings - Fork 11
/
init.ubuntu
193 lines (166 loc) · 5.12 KB
/
init.ubuntu
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
#!/bin/sh
#
## Don't edit this file
## Edit user configuation in /etc/default/autosub to change
##
## Make sure init script is executable
## sudo chmod +x /path/to/init.ubuntu
##
## Install the init script
## sudo ln -s /path/to/init.ubuntu /etc/init.d/autosub
##
## Create the autosub daemon user:
## sudo adduser --system --no-create-home autosub
##
## Make sure /opt/autosub is owned by the autosub user
## sudo chown autosub:nogroup -R /opt/autosub-bootstrapbill
##
## Touch the default file to stop the warning message when starting
## sudo touch /etc/default/autosub
##
## To start AutoSub automatically
## sudo update-rc.d autosub defaults
##
## To start/stop/restart AutoSub
## sudo service autosub start
## sudo service autosub stop
## sudo service autosub restart
##
## Options to include in /etc/default/autosub, uncomment if required
## HP_USER= #$RUN_AS, username to run AutoSub under, the default is autosub
## HP_HOME= #$APP_PATH, the location of AutoSub.py, the default is /opt/autosub-bootstrapbill
## HP_PIDFILE= #$PID_FILE, the location of auto.pid, the default is /opt/autosub-bootstrapbill/autosub.pid
## PYTHON_BIN= #$DAEMON, the location of the python binary, the default is /usr/bin/python
## HP_OPTS= #$EXTRA_DAEMON_OPTS, extra cli option for autosub, i.e. " --config=/opt/autosub-bootstrapbill/config.properties --nolaunch --daemon"
## SSD_OPTS= #$EXTRA_SSD_OPTS, extra start-stop-daemon option like " --group=users"
##
## EXAMPLE if want to run as different user
## add HP_USER=username to /etc/default/autosub
## otherwise default autosub is used
#
### BEGIN INIT INFO
# Provides: autosub
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Should-Start: $NetworkManager
# Should-Stop: $NetworkManager
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts instance of AutoSub
# Description: starts instance of AutoSub using start-stop-daemon
### END INIT INFO
# Script name
NAME=autosub
# App name
DESC=AutoSub
SETTINGS_LOADED=FALSE
. /lib/lsb/init-functions
# Source AutoSub configuration
if [ -f /etc/default/autosub ]; then
SETTINGS=/etc/default/autosub
else
log_warning_msg "/etc/default/autosub not found using default settings.";
fi
check_retval() {
if [ $? -eq 0 ]; then
log_end_msg 0
return 0
else
log_end_msg 1
exit 1
fi
}
load_settings() {
if [ $SETTINGS_LOADED != "TRUE" ]; then
. $SETTINGS
## The defaults
# Run as username
RUN_AS=${HP_USER-autosub}
# Path to app HP_HOME=path_to_app_AutoSub.py
APP_PATH=${HP_HOME-/opt/autosub-bootstrapbill/}
# Path to store PID file
PID_FILE=${HP_PIDFILE-/opt/autosub-bootstrapbill/autosub.pid}
# Path to python bin
DAEMON=${PYTHON_BIN-/usr/bin/python}
# Extra daemon option like: HP_OPTS=" "
EXTRA_DAEMON_OPTS=${HP_OPTS--c/opt/autosub-bootstrapbill/config.properties -d -l}
# Extra start-stop-daemon option like START_OPTS=" --group=users"
EXTRA_SSD_OPTS=${SSD_OPTS-}
DAEMON_OPTS=" AutoSub.py ${EXTRA_DAEMON_OPTS}"
SETTINGS_LOADED=TRUE
fi
[ -x $DAEMON ] || {
log_warning_msg "$DESC: Can't execute daemon, aborting. See $DAEMON";
return 1;}
return 0
}
load_settings || exit 0
is_running () {
# returns 1 when running, else 0.
if [ -e $PID_FILE ]; then
PID=`cat $PID_FILE`
RET=$?
[ $RET -gt 1 ] && exit 1 || return $RET
else
return 1
fi
}
handle_pid () {
PID_PATH=`dirname $PID_FILE`
[ -d $PID_PATH ] || mkdir -p $PID_PATH && chown -R $RUN_AS $PID_PATH > /dev/null || {
log_warning_msg "$DESC: Could not create $PID_FILE, See $SETTINGS, aborting.";
return 1;}
if [ -e $PID_FILE ]; then
PID=`cat $PID_FILE`
if ! kill -0 $PID > /dev/null 2>&1; then
log_warning_msg "Removing stale $PID_FILE"
rm $PID_FILE
fi
fi
}
handle_updates () {
chown -R $RUN_AS $APP_PATH > /dev/null || {
log_warning_msg "$DESC: $APP_PATH not writable by $RUN_AS for web-updates";
return 0; }
}
start_autosub () {
handle_pid
handle_updates
if ! is_running; then
log_daemon_msg "Starting $DESC"
start-stop-daemon -o -d $APP_PATH -c $RUN_AS --start $EXTRA_SSD_OPTS --pidfile $PID_FILE --exec $DAEMON -- $DAEMON_OPTS
check_retval
else
log_success_msg "$DESC: already running (pid $PID)"
fi
}
stop_autosub () {
if is_running; then
log_daemon_msg "Stopping $DESC"
start-stop-daemon -o --stop --pidfile $PID_FILE --retry 15
check_retval
else
log_success_msg "$DESC: not running"
fi
}
case "$1" in
start)
start_autosub
;;
stop)
stop_autosub
;;
restart|force-reload)
stop_autosub
start_autosub
;;
status)
status_of_proc -p "$PID_FILE" "$DAEMON" "$DESC"
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
exit 1
;;
esac
exit 0