@@ -67,6 +67,13 @@ if ! [ $1 == "install" ]; then
67
67
WRITE_DIR=$( dirname " $( echo ` grep " ^write-data=" " $FCONF " ` | cut -d' =' -f2 | sed -e ' s#__PATH__executable__#' $( dirname " $BINARY " ) /..' #g' ) " )
68
68
fi
69
69
debug " write path: $WRITE_DIR "
70
+
71
+ PIDFILE=" ${WRITE_DIR} /server.pid"
72
+
73
+ if [ -z " ${FIFO} " ]; then
74
+ FIFO=" ${WRITE_DIR} /server.fifo"
75
+ fi
76
+
70
77
fi
71
78
72
79
usage (){
@@ -77,10 +84,10 @@ usage(){
77
84
echo -e " stop \t\t Stops the server"
78
85
echo -e " restart \t\t Restarts the server"
79
86
echo -e " status \t\t Displays server status"
87
+ echo -e " cmd command/message \t Send command/chat"
80
88
echo -e " new-game \t\t Stops the server and creates a new game"
81
89
echo -e " save-game name \t Stops the server and saves game to specified save"
82
90
echo -e " load-save name \t Stops the server and loads the specified save"
83
- echo -e " screen \t\t Shows the server screen"
84
91
echo -e " install tarball \t Installs the server with specified tarball"
85
92
echo -e " update [--dry-run] \t Updates the server"
86
93
}
@@ -94,101 +101,120 @@ as_user() {
94
101
fi
95
102
}
96
103
97
- find_pid (){
98
- pid=$( ps ax | grep -v grep | grep " $( echo -e " ${INVOCATION} " | sed -e ' s/[[:space:]]*$//' ) " | grep -v " SCREEN " | awk ' { print $1}' )
99
- debug " assessing pid: $pid "
100
-
101
- if [ " $pid " == " " ]; then
102
- echo " -1"
103
- debug " could not find a pid for binary: \" ${BINARY} \" "
104
- elif [ ` echo " ${pid} " | wc -l` -gt 1 ]; then
105
- echo " -2"
106
- debug " found multiple pids"
107
- else
108
- echo ${pid}
109
- fi
110
- }
111
-
112
104
is_running () {
113
- pid=$( find_pid)
114
- if [ ${pid} -gt 0 ]; then
115
- return 0
116
- elif [ ${pid} == -2 ]; then
117
- echo " Found multiple pids, aborting! (ensure only one instance of $SERVICE_NAME is running)"
118
- exit 1
119
- else
120
- return 1
105
+ if [ -e ${PIDFILE} ]; then
106
+ if kill -0 $( cat ${PIDFILE} ) 2> /dev/null; then
107
+ debug " ${SERVICE_NAME} is running with pid $( cat ${PIDFILE} ) "
108
+ return 0
109
+ else
110
+ debug " Found ${PIDFILE} , but the server is not running. It's possible that your server has crashed"
111
+ debug " Check the log for details"
112
+ rm ${PIDFILE} 2> /dev/null
113
+ return 2
114
+ fi
121
115
fi
116
+ return 1
122
117
}
123
118
124
119
start_service () {
125
- if [ ! -f " $BINARY " ]; then
126
- echo " Failed to start: Can't find the specified binary $BINARY . Please check your config!"
127
- return 1
120
+ if [ -e ${PIDFILE} ]; then
121
+ ps -p $( cat ${PIDFILE} ) > /dev/null 2>&1
122
+ if [ " $? " -eq " 0" ]; then
123
+ echo " ${SERVICE_NAME} is already running!"
124
+ return 1
125
+ fi
126
+ debug " Found rogue pid file, server might have crashed"
127
+ rm ${PIDFILE} 2> /dev/null
128
128
fi
129
129
130
130
if ! check_permissions; then
131
- echo " Error! Incorrect permissions, unable to write to dir \" $ WRITE_DIR\" "
131
+ echo " Error! Incorrect permissions, unable to write to ${ WRITE_DIR} "
132
132
return 1
133
133
fi
134
134
135
- as_user " screen -dmS $SERVICE_NAME $INVOCATION "
136
- if [ $? -eq 0 ]; then
137
- #
138
- # Waiting for the server to start
139
- #
140
- seconds=0
141
- until is_running; do
142
- sleep 1
143
- seconds=$seconds +1
144
- if [[ $seconds -eq 2 ]]; then
145
- echo " Still not running, waiting a while longer..."
146
- fi
147
- if [[ $seconds -ge 10 ]]; then
148
- echo " $SERVICE_NAME failed to start within 10 seconds, giving up"
149
- return 1
150
- fi
151
- done
152
- echo " $SERVICE_NAME is running."
153
- else
154
- echo " Failed to start, ensure SCREEN is installed"
135
+ as_user " tail -f ${FIFO} |${INVOCATION} > /dev/null 2>&1 & echo \$ ! > ${PIDFILE} "
136
+
137
+ ps -p $( cat ${PIDFILE} ) > /dev/null 2>&1
138
+ if [ " $? " -ne " 0" ]; then
139
+ echo " Unable to start ${SERVICE_NAME} "
155
140
return 1
141
+ else
142
+ echo " Started ${SERVICE_NAME} , please see log for details"
156
143
fi
157
144
}
158
145
159
146
stop_service () {
147
+ if [ -e ${PIDFILE} ]; then
148
+ echo -n " Stopping ${SERVICE_NAME} : "
149
+ if kill -TERM $( cat ${PIDFILE} ) 2> /dev/null; then
150
+ sec=1
151
+ while [ " $sec " -le 15 ]; do
152
+ if [ -e ${PIDFILE} ]; then
153
+ if kill -0 $( cat ${PIDFILE} ) 2> /dev/null; then
154
+ echo -n " . "
155
+ sleep 1
156
+ else
157
+ break
158
+ fi
159
+ else
160
+ break
161
+ fi
162
+ sec=$(( $sec + 1 ))
163
+ done
164
+ fi
160
165
161
- #
162
- # Stops the server
163
- #
164
- pid=$( find_pid)
165
- as_user " kill -s 2 ${pid} "
166
- sleep 0.5
167
-
168
- #
169
- # Waiting for the server to shut down
170
- #
171
- seconds=0
172
- while is_running; do
173
- sleep 1
174
- seconds=$seconds +1
175
- if [[ $seconds -eq 2 ]]; then
176
- echo " Still not shut down, waiting a while longer..."
166
+ if kill -0 $( cat ${PIDFILE} ) 2> /dev/null; then
167
+ echo " Unable to shut down nicely, killing the process!"
168
+ kill -KILL $( cat ${PIDFILE} ) 2> /dev/null
169
+ else
170
+ echo " complete!"
177
171
fi
178
- if [[ $seconds -ge 10 ]];
179
- then
180
- echo " $SERVICE_NAME failed to stop within 10 seconds, giving up"
172
+
173
+ rm ${PIDFILE} 2> /dev/null
174
+ return 0 # we've either shut down gracefully or killed the process
175
+ else
176
+ echo " ${SERVICE_NAME} is not running (${PIDFILE} does not exist)"
177
+ return 1
178
+ fi
179
+ }
180
+
181
+ send_cmd (){
182
+ if is_running; then
183
+ if [ -p ${FIFO} ]; then
184
+ echo $@
185
+ echo $@ > ${FIFO}
186
+ sleep 1
187
+ else
188
+ echo " ${FIFO} is not a pipe!"
181
189
return 1
182
190
fi
183
- done
184
-
185
- echo " $SERVICE_NAME is now shut down."
191
+ else
192
+ echo " Unable to send cmd to a stopped server!"
193
+ return 1
194
+ fi
186
195
}
187
196
188
- check_permissions () {
189
- if ! as_user " test -w '$WRITE_DIR '" ; then
190
- debug " Check Permissions. Cannot write to $WRITE_DIR "
191
- return 1
197
+ check_permissions (){
198
+ if [ ! -e " ${BINARY} " ]; then
199
+ echo " Can't find ${BINARY} . Please check your config!"
200
+ exit 1
201
+ fi
202
+
203
+ if ! as_user " test -w ${WRITE_DIR} " ; then
204
+ echo " Check Permissions. Cannot write to ${WRITE_DIR} "
205
+ exit 1
206
+ fi
207
+
208
+ if ! as_user " touch ${PIDFILE} " ; then
209
+ echo " Check Permissions. Cannot touch pidfile ${PIDFILE} "
210
+ exit 1
211
+ fi
212
+
213
+ if ! [ -p ${FIFO} ]; then
214
+ if ! as_user " mkfifo ${FIFO} " ; then
215
+ echo " Failed to create pipe for stdin, if applicable, remove ${FIFO} and try again"
216
+ exit 1
217
+ fi
192
218
fi
193
219
}
194
220
@@ -258,6 +284,7 @@ install(){
258
284
get_bin_version (){
259
285
echo ` as_user " $BINARY --version |egrep '^Version: [0-9\.]+' |egrep -o '[0-9\.]+' |head -n 1" `
260
286
}
287
+
261
288
get_bin_arch (){
262
289
echo ` as_user " $BINARY --version |egrep '^Binary version: ' |egrep -o '[0-9]{2}'" `
263
290
}
@@ -347,6 +374,7 @@ update(){
347
374
348
375
# Stop the server if it is running.
349
376
if is_running; then
377
+ send_cmd " Updating to new Factorio version, be right back"
350
378
stop_service
351
379
fi
352
380
@@ -385,6 +413,7 @@ case "$1" in
385
413
stop)
386
414
# Stops the server
387
415
if is_running; then
416
+ send_cmd " Server is being shut down on request"
388
417
if ! stop_service; then
389
418
echo " Could not stop $SERVICE_NAME "
390
419
exit 1
@@ -398,6 +427,7 @@ case "$1" in
398
427
restart)
399
428
# Restarts the server
400
429
if is_running; then
430
+ send_cmd " Server is being restarted on request, be right back!"
401
431
if stop_service; then
402
432
if ! start_service; then
403
433
echo " Could not start $SERVICE_NAME after restart!"
@@ -425,10 +455,13 @@ case "$1" in
425
455
exit 1
426
456
fi
427
457
;;
428
-
458
+ cmd)
459
+ send_cmd " ${@: 2} "
460
+ ;;
429
461
new-game)
430
462
# Stop Service
431
463
if is_running; then
464
+ send_cmd " Generating new save, please stand by"
432
465
if ! stop_service; then
433
466
echo " Failed to stop server, unable to create new save"
434
467
exit 1
@@ -448,6 +481,7 @@ case "$1" in
448
481
449
482
# Stop Service
450
483
if is_running; then
484
+ send_cmd " Stopping server to save game"
451
485
if ! stop_service; then
452
486
echo " Failed to stop server, unable to save as \" $2 \" "
453
487
exit 1
@@ -470,6 +504,7 @@ case "$1" in
470
504
471
505
# Since stopping the server causes a save we have to stop the server to do this
472
506
if is_running; then
507
+ send_cmd " Stopping server to load a saved game"
473
508
if ! stop_service; then
474
509
echo " Aborting, unable to stop $SERVICE_NAME "
475
510
exit 1
@@ -479,29 +514,6 @@ case "$1" in
479
514
# Touch the new save file
480
515
as_user " touch ${newsave} "
481
516
;;
482
-
483
- screen)
484
- if is_running; then
485
- as_user " script /dev/null -q -c \" screen -rx $SERVICE_NAME \" "
486
- else
487
- echo -n " Server is not running. Do you want to start it? [n]: "
488
- read START_SERVER
489
- case " $START_SERVER " in
490
- [Yy])
491
- if ! start_service; then
492
- echo " Unable to start $SERVICE_NAME "
493
- exit 1
494
- fi
495
- as_user " script /dev/null -q -c \" screen -rx $SERVICE_NAME \" "
496
- ;;
497
- * )
498
- clear
499
- echo " Aborting startup!"
500
- exit 1
501
- ;;
502
- esac
503
- fi
504
- ;;
505
517
install)
506
518
install " $2 "
507
519
;;
0 commit comments