Restart server if it double-forks

Processes that double-fork themselves by double-forking cannot be waited
upon, and cannot be properly monitored.

The ARK server will sometimes restart itself by performing a fork and
execve instead of just an execve (presumably because that is what it has
to do under Windows).

If the server restarts itself, then we need to restart it again so it
can be properly monitored.
This commit is contained in:
Ben Peddell 2023-04-15 02:15:42 +10:00
parent f59a4292cf
commit ef8f3322f3

View File

@ -980,6 +980,8 @@ function getServerPID(){
if kill -0 "$serverpid" >/dev/null 2>&1; then if kill -0 "$serverpid" >/dev/null 2>&1; then
echo "$serverpid" echo "$serverpid"
return return
elif kill -0 "-$serverpid" >/dev/null 2>&1; then
pgrep -g "$serverpid" && return
fi fi
fi fi
if [ -f "${arkserverroot}/${arkserveroldpidfile}" ]; then if [ -f "${arkserverroot}/${arkserveroldpidfile}" ]; then
@ -987,6 +989,8 @@ function getServerPID(){
if kill -0 "$serverpid" >/dev/null 2>&1; then if kill -0 "$serverpid" >/dev/null 2>&1; then
echo "$serverpid" echo "$serverpid"
return return
elif kill -0 "-$serverpid" >/dev/null 2>&1; then
pgrep -g "$serverpid" && return
fi fi
fi fi
} }
@ -1316,13 +1320,16 @@ doRun() {
notify "${notifyMsgShuttingDown:-Shutting down}" notify "${notifyMsgShuttingDown:-Shutting down}"
rm -f "$arkserverroot/$arkautorestartfile" rm -f "$arkserverroot/$arkautorestartfile"
if [ "$serverpid" -ne 0 ]; then if [ "$serverpid" -ne 0 ]; then
kill -INT $serverpid >/dev/null 2>&1 kill -INT -$serverpid >/dev/null 2>&1
fi fi
exit 0 exit 0
} }
trap shutdown_server INT TERM trap shutdown_server INT TERM
# Enable job control so server gets its own process group
set -m
# Auto-restart loop # Auto-restart loop
while [ $restartserver -ne 0 ]; do while [ $restartserver -ne 0 ]; do
echo -n "$(timestamp): Running" echo -n "$(timestamp): Running"
@ -1374,21 +1381,40 @@ doRun() {
echo "$(timestamp): Restarting server" echo "$(timestamp): Restarting server"
notify "${notifyMsgStoppedListening:-Server has stopped listening - restarting}" notify "${notifyMsgStoppedListening:-Server has stopped listening - restarting}"
for (( i = 0; i < 5; i++ )); do for (( i = 0; i < 5; i++ )); do
if ! kill -0 "$serverpid"; then if ! kill -0 "-$serverpid"; then
break break
fi fi
kill -INT "$serverpid" kill -INT "-$serverpid"
sleep 5 sleep 5
done done
if kill -0 "$serverpid"; then if kill -0 "-$serverpid"; then
echo "$(timestamp): Graceful restart failed - killing server" echo "$(timestamp): Graceful restart failed - killing server"
kill -KILL "$serverpid" kill -KILL "-$serverpid"
fi fi
# Exit the server check loop # Exit the server check loop
break break
fi fi
fi fi
elif [ "$pid" == "$(pgrep --pgroup $serverpid)" ]; then
restartserver=1
echo "${timestamp}: Server has double-forked and cannot be effectively monitored"
echo "${timestamp}: Restarting server"
notify "${notifyMsgDoubleForked:-Server has daemonized itself - restarting}"
for (( i = 0; i < 5; i++ )); do
if ! kill -0 "-$serverpid"; then
break
fi
kill -INT "-$serverpid"
sleep 5
done
if kill -0 "-$serverpid"; then
echo "$(timestamp): Graceful restart failed - killing server"
kill -KILL "-$serverpid"
fi
# Exit the server check loop
break
else else
echo "$(timestamp): Bad PID '$pid'; expected '$serverpid'" echo "$(timestamp): Bad PID '$pid'; expected '$serverpid'"
if [ "$pid" != "" ]; then if [ "$pid" != "" ]; then