-
Notifications
You must be signed in to change notification settings - Fork 4
/
run
executable file
·113 lines (98 loc) · 1.93 KB
/
run
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
111
112
113
#!/bin/bash
#Exit on error
set -o errexit
if [[ $(id -u) = "0" ]] && [[ -z "$RUN_AS_USER" ]]
then
echo "Do not run this script as root."
exit 1
fi
pidFile="probe.pid"
logFile="probe.log"
remove_stale() {
echo "Removing stale PID file."
rm "$1"
}
start() {
if [[ -e "$pidFile" ]]
then
if kill -0 $(cat "$pidFile")
then
echo "Probe is already running."
return
else
remove_stale "$pidFile"
fi
fi
echo "Starting probe."
twistd --python=probe.py --logfile="$logFile" --pidfile="$pidFile"
}
stop() {
if [[ -e "$pidFile" ]]
then
if kill -0 $(cat "$pidFile")
then
echo "Stopping probe."
kill -TERM $(cat "$pidFile")
i=0
#twistd removes the pid file on shutdown.
while [[ -e "$pidFile" ]]
do
if (( i++ == 5 ))
then
echo "Waiting..."
i=0
fi
sleep 1
done
echo "Probe stopped".
return
else
remove_stale "$pidFile"
fi
fi
echo "Probe is not running."
}
status() {
if [[ -e "$pidFile" ]]
then
if kill -0 $(cat "$pidFile")
then
echo "Probe is running."
return
fi
fi
#If the PID file does not exist or a process with that PID is not running.
echo "Probe is not running."
}
log() {
tail -F "$logFile"
}
case "$1" in
'start')
start
;;
'stop')
stop
;;
'restart')
stop
start
;;
'status')
status
;;
'console')
stop
start
trap stop EXIT
log
;;
'log')
log
;;
*)
echo "Usage: $0 { console | start | stop | restart | status | log }"
exit 1
;;
esac
exit 0