-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstart.sh
executable file
·51 lines (51 loc) · 1.18 KB
/
start.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
#!/bin/bash
function clean_up {
if [ ! -f bot.pid ]; then
kill -9 $pid
else
kill -9 `cat bot.pid`
fi
echo "Stopping the bot and removing the .pid file"
exit
}
trap clean_up SIGHUP SIGINT SIGTERM
php bot.php & export pid=$!
echo -n "$pid" > bot.pid
echo "The PID of the bot is $pid. If you run this script in the background, to stop it, just remove the bot.pid file created in this folder"
elapsed=0
revived=0
while true; do
if [ "$elapsed" -gt "3600" ]; then
elapsed=0
if [ -f "bot.pid" ]; then
kill -9 $(cat bot.pid)
rm bot.pid
fi
php bot.php & export pid=$!
echo -n "$pid" > bot.pid
fi
if [ ! -f "bot.pid" ]; then
echo "No PID file found"
if [ ! -z "$pid" ]; then
kill -9 "$pid"
exit
fi
fi
alive=$(ps aux | grep "$pid" | grep -v "grep" | wc -l)
if [ "$alive" == "0" ]; then
revived=$((revived+1))
if [ "$revived" -ge "3" ]; then
echo "Bot died 3 times, fix it"
if [ -f bot.pid ]; then
rm bot.pid
fi
exit
fi
echo "Bot dead. Reviving"
php bot.php & export pid=$!
echo -n "$pid" > bot.pid
fi
sleep 1
elapsed=$((elapsed+1))
done
clean_up