-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
init
310 lines (256 loc) · 7.85 KB
/
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
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
#!/bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: nginx LSB init script
# Description: nginx Linux Standards Base compliant init script.
### END INIT INFO
# ----------------------------------------------------------------------------------------------------------------------
# Variables
# ----------------------------------------------------------------------------------------------------------------------
# The name of the daemon.
readonly NAME=nginx
# Arguments that should be passed to the executable.
DAEMON_ARGS=
# The path in which to search for the daemon.
readonly PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# Absolute path to the PID file.
PIDFILE=$(printf -- '%s/run/%s.pid' "$([ -d /run ] || printf -- /var)" "${NAME}")
# Will be set by /lib/init/vars.sh
VERBOSE=no
# ----------------------------------------------------------------------------------------------------------------------
# Error Codes
# ----------------------------------------------------------------------------------------------------------------------
readonly EC_INVALID_ARGUMENT=2
readonly EC_SUPER_USER_ONLY=4
readonly EC_DAEMON_NOT_FOUND=5
readonly EC_RELOADING_FAILED=150
readonly EC_RESTART_STOP_FAILED=151
readonly EC_RESTART_START_FAILED=152
readonly EC_START_FAILED=153
readonly EC_STOP_FAILED=154
# ----------------------------------------------------------------------------------------------------------------------
# Functions
# ----------------------------------------------------------------------------------------------------------------------
# Exit the script with an error code.
die()
{
log_end_msg $1
exit $1
}
# End the script.
end()
{
[ "${VERBOSE}" != no ] && log_end_msg 0
exit 0
}
# Display failure message.
fail()
{
log_failure_msg "${NAME}" "$1"
}
# Display informational message.
info()
{
[ "${VERBOSE}" != no ] && log_daemon_msg "${NAME}" "$1"
}
# Display success message.
ok()
{
[ "${VERBOSE}" != no ] && log_success_msg "${NAME}" "$1"
}
# Display warning message.
warn()
{
log_warning_msg "${NAME}" "$1"
}
# Print usage string.
usage()
{
printf 'Usage: %s {start|stop|restart|try-restart|reload|force-reload|status}\n' "$0"
}
###
# Check privileges of caller. This will automatically exit if the caller has insufficient privileges.
#
# RETURN:
# 0 - sufficient privileges
###
check_privileges()
{
if [ $(id -u) -ne 0 ]
then
fail 'super user only!'
die ${EC_SUPER_USER_ONLY}
fi
}
###
# Reloads the service.
#
# RETURN:
# 0 - successfully reloaded
# 1 - reloading failed
###
reload_service()
{
start-stop-daemon --stop --signal HUP ${SSD_OPTIONS}
}
###
# Starts the service.
#
# RETURN:
# 0 - successfully started
# 1 - starting failed
###
start_service()
{
start-stop-daemon --start ${SSD_OPTIONS} -- ${DAEMON_ARGS}
}
###
# Stops the service.
#
# RETURN:
# 0 - successfully stopped
# 1 - stopping failed
###
stop_service()
{
start-stop-daemon --stop --signal QUIT --retry=TERM/30/KILL/5 ${SSD_OPTIONS}
}
# ----------------------------------------------------------------------------------------------------------------------
# Includes
# ----------------------------------------------------------------------------------------------------------------------
# Load the VERBOSE setting and other rcS variables plus any script defaults.
for INCLUDE in /lib/init/vars.sh /etc/default/${NAME}
do [ -r "${INCLUDE}" ] && . "${INCLUDE}"
done
# Make all variables which are allowed to be altered by the defaults file as read-only.
readonly DAEMON_ARGS
readonly PIDFILE
# ----------------------------------------------------------------------------------------------------------------------
# Bootstrap
# ----------------------------------------------------------------------------------------------------------------------
# Load the LSB log_* functions.
INCLUDE=/lib/lsb/init-functions
if [ -r "${INCLUDE}" ]
then
. "${INCLUDE}"
else
printf '%s: unable to load LSB functions, cannot start service.\n' "${NAME}" 1>&2
exit ${EC_DAEMON_NOT_FOUND}
fi
# Make sure only one argument was passed to the script.
if [ $# -ne 1 ]
then
if [ $# -lt 1 -o "$1" = '' ]
then fail 'action not specified.'
else fail 'too many arguments.'
fi
usage 1>&2
die ${EC_INVALID_ARGUMENT}
fi
readonly ACTION="$1"
# Check if daemon is a recognized program and get absolute path.
readonly DAEMON=$(command -v "${NAME}")
if [ ! -x "${DAEMON}" ]
then
if [ "${ACTION}" = 'stop' ]
then
warn 'executable not found: stop request ignored'
end
else
fail "executable not found: cannot ${ACTION} service"
die ${EC_DAEMON_NOT_FOUND}
fi
fi
# Default options for start-stop-daemon command.
readonly SSD_OPTIONS="--quiet --oknodo --pidfile "${PIDFILE}" --exec "${DAEMON}" --name "${NAME}""
# Determine the service's status.
#
# 0 = Program is running
# 1 = Program is not running and the PID file exists.
# 2 = Program is not running and a LOCK file exists.
# 3 = Program is not running.
# 4 = Unable to determine status.
start-stop-daemon --status ${SSD_OPTIONS} 2>/dev/null 1>/dev/null
readonly STATUS=$?
# ----------------------------------------------------------------------------------------------------------------------
# Handle Input
# ----------------------------------------------------------------------------------------------------------------------
case "$1" in
# Reload the configuration, if the service is running, otherwise do nothing.
force-reload|reload)
if [ ${STATUS} -eq 0 ]
then
info 'reloading configuration ...'
reload_service || die ${EC_RELOADING_FAILED}
else
info 'service not running, nothing to be done.'
fi
;;
# Restart the service, if the service is already running, otherwise start the service.
restart)
if [ ${STATUS} -eq 0 ]
then
info 'restarting service ...'
stop_service || die ${EC_RESTART_STOP_FAILED}
sleep 0.1
fi
start_service || die ${EC_RESTART_START_FAILED}
;;
# Start the service, do nothing if it is already running.
start)
if [ ${STATUS} -eq 0 ]
then
ok 'already started.'
else
info 'starting ...'
start_service || die ${EC_START_FAILED}
fi
;;
# Print service status.
#
# This can be invoked by any user and has different exit codes:
# 0 - running and OK
# 1 - dead and /var/run PID file exists
# 2 - dead and /var/lock lock file exists
# 3 - not running
# 4 - unknown
status)
status_of_proc "${DAEMON}" "${NAME}" || exit $?
;;
# Stop the service, do nothing if it is already stopped.
stop)
if [ ${STATUS} -eq 0 ]
then
info 'stopping ...'
stop_service || die ${EC_STOP_FAILED}
else
info 'already stopped.'
fi
;;
# Restart the service, if the service is already running, otherwise do nothing.
try-restart)
check_privileges
if [ ${STATUS} -eq 0 ]
then
info 'restarting service ...'
stop_service || die ${EC_RESTART_STOP_FAILED}
sleep 0.1
start_service || die ${EC_RESTART_START_FAILED}
else
info 'service not running, nothing to be done.'
fi
;;
-h|--help)
usage
;;
*)
fail "action '${ACTION}' not recognized."
usage 1>&2
exit ${EC_INVALID_ARGUMENT}
;;
esac
end