Newer
Older
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env bash
function help() {
echo "Usage:"
echo " $(basename "$0") COMMAND"
echo ""
echo " Where COMMAND is one of:"
echo " start PIDFILE PROGRAM ARGS... Run PROGRAM and record its PID in PIDFILE"
echo " stop SIGNAL PIDFILE Send SIGNAL to the PID contained in PIDFILE"
echo " help Print this message"
}
function readpid() {
if [ $# -eq 0 ]; then
echo "FATAL: readpid(): Missing pifile" >&2
exit 1
fi
pidfile="$1"
read -r pid <"$pidfile"
echo $pid
}
function run() {
if [ $# -eq 0 ]; then
echo "FATAL: missing program pidfile" >&2
elif [ $# -eq 1 ]; then
echo "FATAL: missing program to run" >&2
help
exit 1
fi
pidfile="$1"
shift
if [ -e "$pidfile" ]; then
pid=$(readpid "$pidfile")
echo "$pid"
if pgrep --pidfile "$pidfile"; then
exit 1
fi
fi
"$@" 2>/dev/null 1>/dev/null 0</dev/null &
pid=$!
echo $pid >"$pidfile"
sleep 1
if ! kill -0 $pid; then
echo "Process $pid does not seem to exist." >&2
echo "The program below may not exist or may have crashed while starting:" >&2
echo " $*" >&2
exit 1
fi
exit 0
}
function stop() {
if [ $# -eq 0 ]; then
echo "FATAL: missing signal" >&2
exit 1
elif [ $# -eq 1 ]; then
echo "FATAL: missing pidfile" >&2
exit 1
fi
signal="$1"
pidfile="$2"
if [ ! -e "$pidfile" ]; then
echo "FATAL: pidfile '$pidfile' does not exist" >&2
exit 1
fi
if pkill "-$signal" --pidfile "$pidfile"; then
rm "$pidfile"
exit 0
fi
exit 1
}
if [ $# -eq 0 ]; then
echo "FATAL: missing arguments" >&2
help
exit 1
fi
case $1 in
start)
shift
run "$@"
;;
stop)
shift
stop "$@"
;;
help)
help
exit 0
;;
esac