This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sentinel.sh
executable file
·73 lines (60 loc) · 1.62 KB
/
sentinel.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
67
68
69
70
71
72
73
#!/bin/bash
# How many seconds to wait between heartbeat checks
HEARTRATE=300
# How many times to retry before restarting the bot
RETRIES=3
# This makes sure the bot is shutdown if
# the sentinel script itself is killed or interrupted
__cleanup()
{
SIGNAL=$1
# Kill the bot, if it is still running
if [[ $(ps -p $pid) == *"python"* ]]
then
kill -15 $pid
fi
# when this function was called due to receiving a signal
# disable the previously set trap and kill yourself with
# the received signal
if [ -n "$SIGNAL" ]
then
trap $SIGNAL
kill -${SIGNAL} $$
fi
}
trap '__cleanup 1' HUP
trap '__cleanup 2' INT
trap '__cleanup 3' QUIT
trap '__cleanup 13' PIPE
trap '__cleanup 15' TERM
# Change to dir that contains this script
cd "$(dirname "$0")"
while true
do
echo "Starting bot"
./run.sh &
# Short wait to make sure the python interpreter is running
sleep 1
pid=$(ps x | grep app.py | grep python | awk '{print $1}')
echo "Bot running with PID $pid"
# Give the bot some time to start and write the first timestamp
sleep 10
timestamp=$(cat DanMemoDiscordBot/heartbeat-file.txt)
count=0
while [ "$count" -lt $RETRIES ]
do
sleep $HEARTRATE
new_timestamp=$(cat DanMemoDiscordBot/heartbeat-file.txt)
if [ $timestamp -eq $new_timestamp ]
then
count=$((count+1))
echo "No heartbeat. Retrying $count..."
fi
timestamp=$new_timestamp
done
# Kill the bot, if it is still running
if [[ $(ps -p $pid) == *"python"* ]]
then
kill -15 $pid
fi
done