Skip to content

Commit

Permalink
Restart server if it double-forks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
klightspeed committed Apr 14, 2023
1 parent f59a429 commit ef8f332
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions tools/arkmanager
Original file line number Diff line number Diff line change
Expand Up @@ -980,13 +980,17 @@ function getServerPID(){
if kill -0 "$serverpid" >/dev/null 2>&1; then
echo "$serverpid"
return
elif kill -0 "-$serverpid" >/dev/null 2>&1; then
pgrep -g "$serverpid" && return
fi
fi
if [ -f "${arkserverroot}/${arkserveroldpidfile}" ]; then
serverpid="$(<"${arkserverroot}/${arkserveroldpidfile}")"
if kill -0 "$serverpid" >/dev/null 2>&1; then
echo "$serverpid"
return
elif kill -0 "-$serverpid" >/dev/null 2>&1; then
pgrep -g "$serverpid" && return
fi
fi
}
Expand Down Expand Up @@ -1316,13 +1320,16 @@ doRun() {
notify "${notifyMsgShuttingDown:-Shutting down}"
rm -f "$arkserverroot/$arkautorestartfile"
if [ "$serverpid" -ne 0 ]; then
kill -INT $serverpid >/dev/null 2>&1
kill -INT -$serverpid >/dev/null 2>&1
fi
exit 0
}
trap shutdown_server INT TERM
# Enable job control so server gets its own process group
set -m
# Auto-restart loop
while [ $restartserver -ne 0 ]; do
echo -n "$(timestamp): Running"
Expand Down Expand Up @@ -1374,21 +1381,40 @@ doRun() {
echo "$(timestamp): Restarting server"
notify "${notifyMsgStoppedListening:-Server has stopped listening - restarting}"
for (( i = 0; i < 5; i++ )); do
if ! kill -0 "$serverpid"; then
if ! kill -0 "-$serverpid"; then
break
fi
kill -INT "$serverpid"
kill -INT "-$serverpid"
sleep 5
done
if kill -0 "$serverpid"; then
if kill -0 "-$serverpid"; then
echo "$(timestamp): Graceful restart failed - killing server"
kill -KILL "$serverpid"
kill -KILL "-$serverpid"
fi
# Exit the server check loop
break
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
echo "$(timestamp): Bad PID '$pid'; expected '$serverpid'"
if [ "$pid" != "" ]; then
Expand Down

0 comments on commit ef8f332

Please sign in to comment.