dot

packages and services management
Log | Files | Refs | README

commit dedfc7f6a32bdfa13769834e24507c12d10ab606
parent 6d5a001a7a2230a50f2cf1ad60b463847447d231
Author: Josuah Demangeon <josuah.demangeon@gandi.net>
Date:   Tue, 15 Aug 2017 12:57:28 +0200

pkg/bin/pkg-service: handle services crashing

The output is also leaner, easier to read.

When a service crash (when /proc/*/cmdline and ~/run/pid/*/* differ),
it gets notified with a '[!]' on next run of pkg-service.  This enables
future implementation to add */crash hooks.

Diffstat:
Dpkg/bin/.pkg-service.swp | 0
Mpkg/bin/pkg-service | 51++++++++++++++++++++++++++++++++++++++-------------
2 files changed, 38 insertions(+), 13 deletions(-)

diff --git a/pkg/bin/.pkg-service.swp b/pkg/bin/.pkg-service.swp Binary files differ. diff --git a/pkg/bin/pkg-service b/pkg/bin/pkg-service @@ -1,20 +1,28 @@ cmd=$1 srv=${2%/} error=0 running=0 acted=0 case "$cmd" in -(start | stop | restart | status) +(start | stop | status) shift 2 ;; +(restart) + "$0" stop && "$0" start || exit 1 + ;; ('') cd "$PKG" || exit 1 for service in */run do "$0" status "${service%/run}" done exit 0 + ;; (*) printf '%s (start | stop | restart | status) <pkg>\n' "${0##*/}" + ;; esac -mkdir -p "$RUN/pid" "$RUN/log" +mkdir -p "$RUN/pid" + + +# --- stop / status ------------------------------------------------------------ [ -d "$RUN/pid/$srv" ] && for pid in $(ls "$RUN/pid/$srv") do @@ -26,32 +34,49 @@ do then acted=1 cmdline="$(tr '\0' ' ' < "$RUN/pid/$srv/$pid")" - if [ "$cmd" = stop ] || [ "$cmd" = restart ] + if [ "$cmd" = stop ] then kill "$pid" && - printf '%-15s [stopped] %s\n' "$srv" "$cmdline" + printf '%-15s [x] %s\n' "$srv" "$cmdline" else running=1 - printf '%-15s [running] %s\n' "$srv" "$cmdline" + printf '%-15s [r] %s\n' "$srv" "$cmdline" fi fi done -[ "$acted""$cmd" = '0''status' ] && printf '%-15s [stopped]\n' "$srv" +if [ "$acted""$cmd" = '0''status' ] || [ "$acted""$cmd" = '0''stop' ] +then printf '%-15s [ ]\n' "$srv" +fi + -if [ "$running" = 0 ] && [ "$cmd" = start ] || [ "$cmd" = restart ] +# --- start -------------------------------------------------------------------- + +if [ "$running" = 0 ] && [ "$cmd" = start ] then mkdir -p "$RUN/pid/$srv" - . "$PKG/$srv/run" "$@" >> "$RUN/log/$srv" 2>&1 && - cp "/proc/$PID/cmdline" "$RUN/pid/$srv/$PID" || exit 1 + . "$PKG/$srv/run" "$@" >> "$VAR/log/$srv" 2>&1 <&- - printf '%-15s [started] ' "$srv" - tr '\0' ' ' < "$RUN/pid/$srv/$PID" - printf '\n' + if cp "/proc/$PID/cmdline" "$RUN/pid/$srv/$PID" 2> /dev/null + then + cmdline=$(tr '\0' ' ' < "$RUN/pid/$srv/$PID") + printf '%-15s [+] %s\n' "$srv" "$cmdline" + else + touch "$RUN/pid/$srv/$PID" + fi fi + +# --- check -------------------------------------------------------------------- + find "$RUN/pid" -type f | while IFS='' read path -do cmp "$path" "/proc/${path##*/}/cmdline" 2> /dev/null || rm -f "$path" +do + if ! cmp "$path" "/proc/${path##*/}/cmdline" 2> /dev/null + then + srv=${path%/*} srv=${srv##*/} + printf '%-15s [!] %s\n' "$srv" "$(tr '\0' ' ' < "$path")" + rm -f "$path" + fi done find "$RUN/pid" -depth -type d -exec rmdir {} + 2> /dev/null || true