-
Notifications
You must be signed in to change notification settings - Fork 14
/
start-freya.sh
executable file
·275 lines (258 loc) · 6.37 KB
/
start-freya.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/bin/sh
# Freya Start Script
# Starts freya, stops it, etc...
# $Id: start-freya.sh 630 2006-06-05 07:58:18Z MagicalTux $
#
# This script uses itself as a daemon to make sure
# no server stops. It will check every 10 secs that
# each server is running by sending a "SIGUSR1" to
# the server.
#
# Change this value to reflect your installation path if the resident can't locate Freya
FREYA_PATH=.
# Check our directory
if [ ! -f "$FREYA_PATH/login-server" ]; then
# login-server isn't with us, try to locate it in the path found in $0
POSSIBLE_PATH=`dirname "$0"`
if [ ! -f "$POSSIBLE_PATH/login_server" ]; then
echo "Server not found, make sure that $0 is ran within Freya directory."
exit 1
fi
FREYA_PATH="$POSSIBLE_PATH"
fi
# CONFIG
FREYA_BIN="login-server char-server map-server"
UPDATE_STATUS_TIMER=60
PIDFILE_PATH="$FREYA_PATH/save/agent.pid"
STFILE_PATH="$FREYA_PATH/save/agent_status.txt"
LOGFILE_PATH="$FREYA_PATH/log/agent.log"
DAEMON_STOP_TIMEOUT="2s"
freya_start() {
if [ -f "$PIDFILE_PATH" ]; then
if kill -0 `cat "$PIDFILE_PATH"` 2>/dev/null; then
echo "Error: resident seems to be already running. Erase $PIDFILE_PATH if it's not the case."
exit 1
fi
# stale pidfile
rm -f "$PIDFILE_PATH"
fi
echo -n "Starting Freya resident..."
freya_resident &
PID="$!"
echo "$PID" >"$PIDFILE_PATH"
echo " started (pid: $PID)"
}
freya_check() {
if [ -f "$PIDFILE_PATH" ]; then
if kill -0 `cat "$PIDFILE_PATH"` 2>/dev/null; then
# already running, nothing to do
exit 0
fi
# stale pidfile
rm -f "$PIDFILE_PATH"
fi
# start freya resident...
freya_resident &
PID="$!"
echo "$PID" >"$PIDFILE_PATH"
}
freya_restart() {
if [ -f "$PIDFILE_PATH" ]; then
if kill -USR2 `cat "$PIDFILE_PATH"` 2>/dev/null; then
echo "Restart signal sent. Server is restarting..."
exit
fi
fi
echo "Failed to send Restart signal. Try to start the server before restarting it..."
exit 1
}
freya_stop() {
if [ -f "$PIDFILE_PATH" ]; then
PID=`cat "$PIDFILE_PATH"`
if kill -TERM $PID 2>/dev/null; then
echo -n "Please wait while stopping Freya..."
TIMEOUT=15
while true; do
sleep 1s
if kill -0 $PID 2>/dev/null; then
# still running -> wait
if [ $TIMEOUT -lt 1 ]; then
echo "FAILED"
echo "Stopping of Freya timed out. Please check ps"
exit 1
fi
else
break
fi
TIMEOUT=$[ $TIMEOUT - 1 ]
done
echo "OK"
exit
fi
fi
echo "Could not stop Freya. It's most likely that Freya isn't running currently."
exit 1
}
freya_res_log() {
echo "["`date`"] $@" >>"$LOGFILE_PATH"
}
freya_status() {
DAEMON=`cat "$STFILE_PATH"`
NOW=`date +%s`
echo -e "Name\t\tState\tPID\tUptime"
for foo in $DAEMON; do
D_NAME=`echo "$foo" | cut -d'|' -f1`
D_START=`echo "$foo" | cut -d'|' -f2`
D_PID=`echo "$foo" | cut -d'|' -f3`
if kill -0 $D_PID 2>/dev/null; then
D_RUNNING="up"
else
D_RUNNING="down"
fi
UPTIME=$[ $NOW - $D_START ]
echo -e "$D_NAME\t$D_RUNNING\t$D_PID\t${UPTIME}s"
done
}
freya_res_stop() {
freya_res_log "Ending signal received, stopping daemons..."
STOP_DAEMON=""
# reverse order to stop map first
for foo in $DAEMON; do STOP_DAEMON="$foo $STOP_DAEMON"; done
NOW=`date +%s`
for foo in $STOP_DAEMON; do
D_NAME=`echo "$foo" | cut -d'|' -f1`
D_START=`echo "$foo" | cut -d'|' -f2`
D_PID=`echo "$foo" | cut -d'|' -f3`
if [ x$D_PID = x ]; then continue; fi
UPTIME=$[ $NOW - $D_START ]
freya_res_log "Stopping $D_NAME after an uptime of $UPTIME secs"
if kill -TERM $D_PID 2>/dev/null; then
sleep "$DAEMON_STOP_TIMEOUT"
if kill -KILL $D_PID 2>/dev/null; then
freya_res_log "Daemon wasn't stopped - killed with SIGKILL"
fi
fi
done
rm -f "$PIDFILE_PATH"
exit 0
}
freya_res_restart() {
freya_res_log "Restarting all FREYA servers..."
STOP_DAEMON=""
# reverse order to stop map first
for foo in $DAEMON; do STOP_DAEMON="$foo $STOP_DAEMON"; done
NOW=`date +%s`
for foo in $STOP_DAEMON; do
D_NAME=`echo "$foo" | cut -d'|' -f1`
D_START=`echo "$foo" | cut -d'|' -f2`
D_PID=`echo "$foo" | cut -d'|' -f3`
if [ x$D_PID = x ]; then continue; fi
UPTIME=$[ $NOW - $D_START ]
freya_res_log "Stopping $D_NAME after an uptime of $UPTIME secs"
if kill -TERM $D_PID 2>/dev/null; then
sleep "$DAEMON_STOP_TIMEOUT"
if kill -KILL $D_PID 2>/dev/null; then
freya_res_log "Daemon wasn't stopped - killed with SIGKILL"
fi
fi
done
# reset DAEMON to avoid warning messages...
DAEMON=""
for foo in $FREYA_BIN; do
DAEMON="$DAEMON ${foo}|0|"
done
}
freya_resident() {
# Freya Resident System
cd $FREYA_PATH
DAEMON=""
# close stdin, stdout and stderr
exec 0</dev/null
exec 1>/dev/null
exec 2>/dev/null
freya_res_log "Freya Resident v1.0 by MagicalTux <[email protected]>"
trap freya_res_stop SIGINT
trap freya_res_stop SIGTERM
trap freya_res_restart SIGUSR2
for foo in $FREYA_BIN; do
if [ -x $foo ]; then
DAEMON="$DAEMON ${foo}|0|"
else
echo "[ERROR] Missing binary : $foo"
exit 1
fi
done
# save start time for uptime statistics...
RESIDENT=`date +%s`
ALIVE_STATUS=$[ $RESIDENT - 60 ]
# main loop : check daemons every 10 secs
while true; do
NEW_DAEMON=""
NOW=`date +%s`
ACT_UPDATE_STATUS=no
if [ $NOW -gt $ALIVE_STATUS ]; then
ALIVE_STATUS=$[ $NOW + $UPDATE_STATUS_TIMER ]
ACT_UPDATE_STATUS="yes"
fi
for foo in $DAEMON; do
D_NAME=`echo "$foo" | cut -d'|' -f1`
D_START=`echo "$foo" | cut -d'|' -f2`
D_PID=`echo "$foo" | cut -d'|' -f3`
UPTIME=$[ $NOW - $D_START ]
if [ x$D_PID = x ]; then
# start daemon !
freya_res_log "Starting $D_NAME ..."
./$D_NAME >/dev/null 2>&1 &
D_PID=$!
D_START=`date +%s`
UPTIME=0
else
# check if it's still running !
if kill -0 $D_PID 2>/dev/null; then
# ok, good !
:
else
# argh !!
freya_res_log "Warning: $D_NAME down after $UPTIME secs - restarting !"
./$D_NAME >/dev/null 2>&1 &
D_PID=$!
D_START=$NOW
UPTIME=0
fi
fi
NEW_DAEMON="$NEW_DAEMON ${D_NAME}|${D_START}|${D_PID}"
done
# copy & strip spaces
DAEMON=`echo $NEW_DAEMON`
if [ x"$ACT_UPDATE_STATUS" != x"no" ]; then
echo "$DAEMON" >"$STFILE_PATH"
fi
# sleep a bit before re-running the tests....
sleep 10s
done
}
case "$1" in
start)
freya_start
;;
restart)
freya_restart
;;
stop)
freya_stop
;;
status)
freya_status
;;
resident)
freya_resident
;;
check)
freya_check
;;
*)
echo "Usage: $0 ( start | stop | restart | status | check | resident )"
exit 1
;;
esac
exit 0