2 # service: simple wrapper around start-stop-daemon
4 # Usage: service ACTION EXEC ARGS...
7 # -C check if EXEC is alive
8 # -S start EXEC, passing it ARGS as its arguments
9 # -K kill EXEC, sending it a TERM signal if not specified otherwise
11 # Environment variables exposed:
12 # SERVICE_DAEMONIZE run EXEC in background
13 # SERVICE_WRITE_PID create a pid-file and use it for matching
14 # SERVICE_MATCH_EXEC use EXEC command-line for matching (default)
15 # SERVICE_MATCH_NAME use EXEC process name for matching
16 # SERVICE_USE_PID assume EXEC create its own pid-file and use it for matching
17 # SERVICE_NAME process name to use (default to EXEC file part)
18 # SERVICE_PID_FILE pid file to use (default to /var/run/$SERVICE_NAME.pid)
19 # SERVICE_SIG signal to send when using -K
20 # SERVICE_SIG_RELOAD default signal used when reloading
21 # SERVICE_SIG_STOP default signal used when stopping
22 # SERVICE_STOP_TIME time to wait for a process to stop gracefully before killing it
23 # SERVICE_UID user EXEC should be run as
24 # SERVICE_GID group EXEC should be run as
26 # SERVICE_DEBUG don't do anything, but show what would be done
27 # SERVICE_QUIET don't print anything
31 SERVICE_SIG_RELOAD
="HUP"
32 SERVICE_SIG_STOP
="TERM"
41 ssd
="${SERVICE_DEBUG:+echo }start-stop-daemon${SERVICE_QUIET:+ -q}"
47 ssd
="$ssd -S${SERVICE_DAEMONIZE:+ -b}${SERVICE_WRITE_PID:+ -m}"
51 ssd
="$ssd -K${SERVICE_SIG:+ -s $SERVICE_SIG}"
54 echo "service: unknown ACTION '$1'" 1>&2
60 echo "service: missing argument" 1>&2
64 echo "service: file '$exec' is not executable" 1>&2
67 name
="${SERVICE_NAME:-${exec##*/}}"
68 [ -z "$SERVICE_USE_PID$SERVICE_WRITE_PID$SERVICE_PID_FILE" ] \
69 || ssd
="$ssd -p ${SERVICE_PID_FILE:-/var/run/$name.pid}"
70 [ -z "$SERVICE_MATCH_NAME" ] || ssd
="$ssd -n $name"
71 ssd
="$ssd${SERVICE_UID:+ -c $SERVICE_UID${SERVICE_GID:+:$SERVICE_GID}}"
72 [ -z "$SERVICE_MATCH_EXEC$start" ] || ssd
="$ssd -x $exec"
82 SERVICE_SIG
="${SERVICE_SIG:-USR1}" service
-K "$@"
91 SERVICE_SIG
="${SERVICE_SIG:-$SERVICE_SIG_STOP}" service
-K "$@" ||
return 1
92 while [ $
((try
++)) -lt $SERVICE_STOP_TIME ]; do
93 service
-C "$@" ||
return 0
96 SERVICE_SIG
="KILL" service
-K "$@"
102 SERVICE_SIG
="${SERVICE_SIG:-$SERVICE_SIG_RELOAD}" service
-K "$@"
106 cat 1>&2 << __END_OF_WARNING__
108 # WARNING: the 'service_kill' function is now deprecated and might be
109 # removed soon. Consider using the other new service_* wrappers instead.
113 local pid
="${2:-$(pidof "$name")}"
114 local grace
="${3:-5}"
116 [ -f "$pid" ] && pid
="$(head -n1 "$pid" 2>/dev/null)"
119 [ -d "/proc/$pid" ] ||
continue
121 kill -TERM $pid 2>/dev
/null
&& \
122 while grep -qs "$name" "/proc/$pid/cmdline" && [ $
((try
++)) -lt $grace ]; do sleep 1; done
123 kill -KILL $pid 2>/dev
/null
&& \
124 while grep -qs "$name" "/proc/$pid/cmdline"; do sleep 1; done
This page took 0.048437 seconds and 5 git commands to generate.