summaryrefslogtreecommitdiff
path: root/sys-apps/shepherd/files/shepherd.initd
blob: c56b518fe15c4fdd45e52406635bd0154e673b5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

NAME=shepherd
DAEMON=/usr/bin/shepherd
PIDFILE=/run/shepherd.pid
CONFIG=/etc/shepherd.scm

case "$1" in
    start)
        echo "Starting Shepherd service manager..."

        if [ -f "$PIDFILE" ] &&
           kill -0 "$(cat "$PIDFILE")" 2>/dev/null
        then
            echo "Shepherd is already running."
            exit 0
        fi

        mkdir -p /run/shepherd
        chmod 755 /run/shepherd

        "$DAEMON" \
            --config="$CONFIG" \
            --pid="$PIDFILE"

        exit $?
        ;;

    stop)
        echo "Stopping Shepherd service manager..."

        if [ ! -f "$PIDFILE" ]; then
            echo "Shepherd is not running."
            exit 0
        fi

        PID="$(cat "$PIDFILE")"

        if kill -0 "$PID" 2>/dev/null; then
            kill -TERM "$PID"

            i=0
            while kill -0 "$PID" 2>/dev/null; do
                i=$((i + 1))

                if [ "$i" -ge 50 ]; then
                    kill -KILL "$PID" 2>/dev/null
                    break
                fi

                sleep 0.1
            done
        fi

        rm -f "$PIDFILE"

        exit 0
        ;;

    restart)
        "$0" stop
        "$0" start
        ;;

    status)
        if [ -f "$PIDFILE" ] &&
           kill -0 "$(cat "$PIDFILE")" 2>/dev/null
        then
            echo "Shepherd is running (PID $(cat "$PIDFILE"))."
            exit 0
        fi

        echo "Shepherd is not running."
        exit 3
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac