@@ -67,6 +67,13 @@ if ! [ $1 == "install" ]; then
6767 WRITE_DIR=$( dirname " $( echo ` grep " ^write-data=" " $FCONF " ` | cut -d' =' -f2 | sed -e ' s#__PATH__executable__#' $( dirname " $BINARY " ) /..' #g' ) " )
6868 fi
6969 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+
7077fi
7178
7279usage (){
@@ -77,10 +84,10 @@ usage(){
7784 echo -e " stop \t\t Stops the server"
7885 echo -e " restart \t\t Restarts the server"
7986 echo -e " status \t\t Displays server status"
87+ echo -e " cmd command/message \t Send command/chat"
8088 echo -e " new-game \t\t Stops the server and creates a new game"
8189 echo -e " save-game name \t Stops the server and saves game to specified save"
8290 echo -e " load-save name \t Stops the server and loads the specified save"
83- echo -e " screen \t\t Shows the server screen"
8491 echo -e " install tarball \t Installs the server with specified tarball"
8592 echo -e " update [--dry-run] \t Updates the server"
8693}
@@ -94,101 +101,120 @@ as_user() {
94101 fi
95102}
96103
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-
112104is_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
121115 fi
116+ return 1
122117}
123118
124119start_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
128128 fi
129129
130130 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} "
132132 return 1
133133 fi
134134
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} "
155140 return 1
141+ else
142+ echo " Started ${SERVICE_NAME} , please see log for details"
156143 fi
157144}
158145
159146stop_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
160165
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!"
177171 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!"
181189 return 1
182190 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
186195}
187196
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
192218 fi
193219}
194220
@@ -258,6 +284,7 @@ install(){
258284get_bin_version (){
259285 echo ` as_user " $BINARY --version |egrep '^Version: [0-9\.]+' |egrep -o '[0-9\.]+' |head -n 1" `
260286}
287+
261288get_bin_arch (){
262289 echo ` as_user " $BINARY --version |egrep '^Binary version: ' |egrep -o '[0-9]{2}'" `
263290}
@@ -347,6 +374,7 @@ update(){
347374
348375 # Stop the server if it is running.
349376 if is_running; then
377+ send_cmd " Updating to new Factorio version, be right back"
350378 stop_service
351379 fi
352380
@@ -385,6 +413,7 @@ case "$1" in
385413 stop)
386414 # Stops the server
387415 if is_running; then
416+ send_cmd " Server is being shut down on request"
388417 if ! stop_service; then
389418 echo " Could not stop $SERVICE_NAME "
390419 exit 1
@@ -398,6 +427,7 @@ case "$1" in
398427 restart)
399428 # Restarts the server
400429 if is_running; then
430+ send_cmd " Server is being restarted on request, be right back!"
401431 if stop_service; then
402432 if ! start_service; then
403433 echo " Could not start $SERVICE_NAME after restart!"
@@ -425,10 +455,13 @@ case "$1" in
425455 exit 1
426456 fi
427457 ;;
428-
458+ cmd)
459+ send_cmd " ${@: 2} "
460+ ;;
429461 new-game)
430462 # Stop Service
431463 if is_running; then
464+ send_cmd " Generating new save, please stand by"
432465 if ! stop_service; then
433466 echo " Failed to stop server, unable to create new save"
434467 exit 1
@@ -448,6 +481,7 @@ case "$1" in
448481
449482 # Stop Service
450483 if is_running; then
484+ send_cmd " Stopping server to save game"
451485 if ! stop_service; then
452486 echo " Failed to stop server, unable to save as \" $2 \" "
453487 exit 1
@@ -470,6 +504,7 @@ case "$1" in
470504
471505 # Since stopping the server causes a save we have to stop the server to do this
472506 if is_running; then
507+ send_cmd " Stopping server to load a saved game"
473508 if ! stop_service; then
474509 echo " Aborting, unable to stop $SERVICE_NAME "
475510 exit 1
@@ -479,29 +514,6 @@ case "$1" in
479514 # Touch the new save file
480515 as_user " touch ${newsave} "
481516 ;;
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- ;;
505517 install)
506518 install " $2 "
507519 ;;
0 commit comments