-
Notifications
You must be signed in to change notification settings - Fork 0
/
restart_daemon.sh
executable file
·66 lines (58 loc) · 1.64 KB
/
restart_daemon.sh
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
#!/bin/sh
# Many things aren't perfect. Some of those are daemons we need to run 24/7
# despite being written not well enough to not freeze from time to time.
# For those, I wrote this also very much not perfect script. But it should do
# it's job restarting a daemon (run by daemontools) and then checking,
# whether the targeted process actually has a new pid. If not, it may already
# have frozen (ignoring the small chance of hitting the same pid again).
# If the pid didn't change after a graceful restart by daemnontools, we'll just
# kill -9 it. If the pid is still the same after that, we assume something
# went just too wrong to do any more with this script and instead go tell
# someone with a brain.
# I suggest running this as a cron job.
#
# - Alex H.
set -eu
phrase="$1"
service_dir="$HOME/service"
svc_bin='/usr/local/bin/svc'
seconds='2' # Seconds we will wait for the process to be restarted.
function getpid {
pid="$(ps ux | grep -v 'supervise' | grep -e '?' \
| grep -e "${phrase}" | grep -v 'grep' | awk '{print $2}' \
| sort | awk 'NR==1{print $1}')"
case ${n} in
0) pid0="${pid}"
;;
1) pid1="${pid}"
;;
2) pid2="${pid}"
;;
esac
n="$(( ${n} + 1 ))"
}
function restart_daemon {
${svc_bin} -du ${service_dir}/${phrase}
}
n="0"
getpid
restart_daemon
sleep ${seconds}
getpid
if [[ ${pid0} -eq ${pid1} ]]; then
echo "Restarting ${phrase} likely not successfull."
echo "Killing process ${pid0} forcefully."
kill -9 ${pid0}
sleep ${seconds}
getpid
if [[ ${pid0} -eq ${pid2} ]]; then
echo "Still no luck. Check yo'self."
exit 2
else
echo "All good now."
exit 1
fi
else
echo "Restarted ${phrase} cleanly."
fi
exit 0