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