forked from mutantmonkey/phenny
-
Notifications
You must be signed in to change notification settings - Fork 43
/
init.sh
executable file
·113 lines (106 loc) · 2.45 KB
/
init.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
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
#
# bot initscript
#
# Last Updated: Oct 31, 2011
# Modified for Apertium on Dec 28, 2012 and Dec 3, 2019
# If broken, yell at: conor_f, scoopgracie
#
### BEGIN INIT INFO
# Provides: begiak
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Begiak, Apertium's IRC bot
### END INIT INFO
BOT="begiak"
EXEC="/home/begiak/phenny/phenny"
ARGS="--config=/home/begiak/.phenny/default.py -v"
LOGFILE="/home/begiak/logs/begiak.log"
USER="begiak"
start_bot() {
echo starting $BOT
if [ -f "$LOGFILE" ]; then
mv "$LOGFILE" "$LOGFILE.`date +%Y-%m-%d`"
fi;
start-stop-daemon -S -c $USER -p /var/run/$BOT.pid -m -d `dirname $EXEC` -b --startas /bin/bash -- -c "exec $EXEC $ARGS > $LOGFILE 2>&1"
SUCCESS=$?
if [ "$SUCCESS" -gt 0 ]; then
echo "ERROR: Couldn't start $BOT"
fi
return $SUCCESS
}
stop_bot() {
echo stopping $BOT
start-stop-daemon -K -p /var/run/$BOT.pid
if [ "$?" -gt 0 ]; then
echo "WARNING: STOPPING BOT FAILED"
fi
times=0
while [ "$(ps -e | grep -c $(cat /var/run/$BOT.pid))" != 0 ]; do
sleep 1
times=$(($times+1))
if [ ${times-0} -ge 60 ]; then
kill -9 $(cat /var/run/$BOT.pid)
fi
done
times=0
while [ "$(ps -e | grep -c $(cat /var/run/$BOT.pid))" != 0 ]; do
sleep 1
times=$(($times+1))
if [ ${times} -ge 15 ]; then
echo "ERROR: $BOT did not stop"
SUCCESS=1
fi
done
SUCCESS=$?
if [ "$SUCCESS" -gt 0 ]; then
echo "ERROR: Couldn't stop $BOT"
else
mv "$LOGFILE" "$LOGFILE.`date +%Y-%m-%d`"
fi
return $SUCCESS
}
restart_bot() {
stop_bot
if [ "$?" -gt 0 ]; then
echo "WARNING: bot may still be running; restarting anyway in 5 seconds, ^C to cancel"
sleep 5
fi
start_bot
if [ "$?" -gt 0 ]; then
exit 1
fi
}
case "$1" in
start)
start_bot
exit $?
;;
stop)
stop_bot
exit $?
;;
restart)
echo "Restarting $BOT"
restart_bot
exit $?
;;
force-reload)
echo "Restarting $BOT"
restart_bot
exit $?
;;
status)
if [ -e /var/run/$BOT.pid ]; then
return 0
fi
return 3
;;
*)
echo "Usage: /etc/init.d/$BOT {start, stop, restart, force-reload, status}"
exit 1
;;
esac
exit 0