Root/package/base-files/files/lib/functions/service.sh

1#
2# service: simple wrapper around start-stop-daemon
3#
4# Usage: service ACTION EXEC ARGS...
5#
6# Action:
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
10#
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
25#
26# SERVICE_DEBUG don't do anything, but show what would be done
27# SERVICE_QUIET don't print anything
28#
29
30SERVICE_QUIET=1
31SERVICE_SIG_RELOAD="HUP"
32SERVICE_SIG_STOP="TERM"
33SERVICE_STOP_TIME=5
34SERVICE_MATCH_EXEC=1
35
36service() {
37    local ssd
38    local exec
39    local name
40    local start
41    ssd="${SERVICE_DEBUG:+echo }start-stop-daemon${SERVICE_QUIET:+ -q}"
42    case "$1" in
43      -C)
44        ssd="$ssd -K -t"
45        ;;
46      -S)
47        ssd="$ssd -S${SERVICE_DAEMONIZE:+ -b}${SERVICE_WRITE_PID:+ -m}"
48        start=1
49        ;;
50      -K)
51        ssd="$ssd -K${SERVICE_SIG:+ -s $SERVICE_SIG}"
52        ;;
53      *)
54        echo "service: unknown ACTION '$1'" 1>&2
55        return 1
56    esac
57    shift
58    exec="$1"
59    [ -n "$exec" ] || {
60        echo "service: missing argument" 1>&2
61        return 1
62    }
63    [ -x "$exec" ] || {
64        echo "service: file '$exec' is not executable" 1>&2
65        return 1
66    }
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"
73    shift
74    $ssd${1:+ -- "$@"}
75}
76
77service_check() {
78    service -C "$@"
79}
80
81service_signal() {
82    SERVICE_SIG="${SERVICE_SIG:-USR1}" service -K "$@"
83}
84
85service_start() {
86    service -S "$@"
87}
88
89service_stop() {
90    local try
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
94        sleep 1
95    done
96    SERVICE_SIG="KILL" service -K "$@"
97    sleep 1
98    ! service -C "$@"
99}
100
101service_reload() {
102    SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_RELOAD}" service -K "$@"
103}
104
105service_kill() {
106    cat 1>&2 << __END_OF_WARNING__
107#
108# WARNING: the 'service_kill' function is now deprecated and might be
109# removed soon. Consider using the other new service_* wrappers instead.
110#
111__END_OF_WARNING__
112    local name="${1}"
113    local pid="${2:-$(pidof "$name")}"
114    local grace="${3:-5}"
115
116    [ -f "$pid" ] && pid="$(head -n1 "$pid" 2>/dev/null)"
117
118    for pid in $pid; do
119        [ -d "/proc/$pid" ] || continue
120        local try=0
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
125    done
126}
127
128
129
130

Archive Download this file



interactive