dot

packages and services management
Log | Files | Refs | README

commit b205683ecc9e272e6bcb50e02435fb0d169d22b6
parent 3a459aad8cbb8d0439ad3b0cdd1c261b53df65e5
Author: Josuah Demangeon <josuah.demangeon@gandi.net>
Date:   Tue, 20 Jun 2017 22:02:47 +0200

do not split into functions anymore

Diffstat:
MSERVICE | 99+++++++++++++++++++++++++++++++------------------------------------------------
1 file changed, 39 insertions(+), 60 deletions(-)

diff --git a/SERVICE b/SERVICE @@ -1,81 +1,60 @@ # service manager -die() +usage() { - printf '%s\n' "$*" 1>&2 - exit + echo 'service start|stop|restart|status [name]' + exit 1 } -status() -{ - if - [ -f "$VAR/pid/$1" ] && - grep -qF "$1" "/proc/$(cat "$VAR/pid/$1")/cmdline" - then - printf '%-20s [running]\n' "$1" - return 0 - else - printf '%-20s [stopped]\n' "$1" - rm -f "$VAR/$1/pid" - return 1 - fi -} +# --- arguments ---------------------------------------------------------------- +[ $# -lt 2 ] && usage -start() -{ - [ -f "$ETC/$1/start" ] || die "no start script in $ETC/$1" +cmd=$1 srv=$2 +shift 2 - service="$1" - shift +case "$cmd" in ( start | stop | restart | status ) : ;; ( * ) usage ;; esac - status "$service" && return 1 +[ -f "$ETC/$srv/start" ] || echo "no start script in $ETC/$1" && exit 1 - sh "$ETC/$service/start" "$@" > "$VAR/log/$service" 2>&1 && - printf %s $! > "$VAR/pid/$service" - status "$service" -} +# --- main --------------------------------------------------------------------- +mkdir -p "$VAR/pid" "$VAR/log" -stop() -{ - status "$1" || return 1 +return=1 - while read pid arguments - do - tr '\0' ' ' | grep -F "$*" && - kill -14 "$pid" && rm "$VAR/pid/$1" +ls "$VAR/pid/$srv" | while read -r pid arguments +do + for arg in "$@" + do grep -F "$@" "$VAR/pid/$srv/$pid" || continue 2 done - status "$1" && return 1 || return 0 -} - - -usage() -{ - echo 'service start|stop|restart|status [name]' - exit 1 -} - - -# --- main --------------------------------------------------------------------- - -mkdir -p "$VAR/pid" "$VAR/log" - -[ $# -lt 2 ] && usage + if cmp "$VAR/pid/$srv/$pid" "/proc/$pid/cmdline" + then + printf '%-20s [running] ' + tr '\0' ' ' "$VAR/pid/$srv/$pid" + printf '\n' + return=0 + + if [ "$cmd" = stop ] || [ "$cmd" = restart ] + then kill -14 "$pid" && rm "$VAR/pid/$srv/$pid" + fi + fi +done -command=$1 -shift +if [ "$return" = 1 ] +then + printf '%-20s [stopped]\n' "$1" -case "$command" in -( start ) start "$@" ;; -( stop ) stop "$@" ;; -( restart ) stop "$@" && start "$@" ;; -( status ) status "$1" ;; -( * ) usage ;; -esac + if [ "$cmd" = start ] || [ "$cmd" = restart ] + then + mkdir -p "$VAR/pid/$srv" + sh "$ETC/$srv/start" "$@" >> "$VAR/log/$srv" 2>&1 && + cp "/proc/$!/cmdline" "$VAR/pid/$srv/$!" + fi +fi -# we do not execute anything to keep the last exit status +return "$return"