docker-minecraft-server/files/autopause/autopause-daemon.sh

98 lines
2.3 KiB
Bash
Raw Normal View History

2020-05-20 13:17:58 +00:00
#!/bin/bash
. /autopause/autopause-fcns.sh
2020-05-23 14:15:10 +00:00
. /start-utils
2020-05-20 13:17:58 +00:00
sudo /usr/sbin/knockd -c /autopause/knockd-config.cfg -d
if [ $? -ne 0 ] ; then
while :
do
if [[ -n $(ps -o comm | grep java) ]] ; then
break
fi
sleep 0.1
done
2020-05-23 14:15:10 +00:00
logAutopause "Failed to start knockd daemon."
logAutopause "Possible cause: docker's host network mode."
logAutopause "Recreate without host mode or disable autopause functionality."
logAutopause "Stopping server."
2020-05-20 13:17:58 +00:00
killall -SIGTERM java
exit 1
fi
STATE=INIT
while :
do
case X$STATE in
XINIT)
# Server startup
if mc_server_listening ; then
TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_INIT))
2020-05-23 14:15:10 +00:00
logAutopause "MC Server listening for connections - stopping in $AUTOPAUSE_TIMEOUT_INIT seconds"
2020-05-20 13:17:58 +00:00
STATE=K
fi
;;
XK)
# Knocked
if java_clients_connected ; then
2020-05-23 14:15:10 +00:00
logAutopause "Client connected - waiting for disconnect"
2020-05-20 13:17:58 +00:00
STATE=E
else
if [[ $(current_uptime) -ge $TIME_THRESH ]] ; then
2020-05-23 14:15:10 +00:00
logAutopause "No client connected since startup / knocked - stopping"
2020-05-20 13:17:58 +00:00
/autopause/pause.sh
STATE=S
fi
fi
;;
XE)
# Established
if ! java_clients_connected ; then
TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_EST))
2020-05-23 14:15:10 +00:00
logAutopause "All clients disconnected - stopping in $AUTOPAUSE_TIMEOUT_EST seconds"
2020-05-20 13:17:58 +00:00
STATE=I
fi
;;
XI)
# Idle
if java_clients_connected ; then
2020-05-23 14:15:10 +00:00
logAutopause "Client reconnected - waiting for disconnect"
2020-05-20 13:17:58 +00:00
STATE=E
else
if [[ $(current_uptime) -ge $TIME_THRESH ]] ; then
2020-05-23 14:15:10 +00:00
logAutopause "No client reconnected - stopping"
2020-05-20 13:17:58 +00:00
/autopause/pause.sh
STATE=S
fi
fi
;;
XS)
# Stopped
if rcon_client_exists ; then
/autopause/resume.sh
fi
if java_running ; then
if java_clients_connected ; then
2020-05-23 14:15:10 +00:00
logAutopause "Client connected - waiting for disconnect"
2020-05-20 13:17:58 +00:00
STATE=E
else
TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_KN))
2020-05-23 14:15:10 +00:00
logAutopause "Server was knocked - waiting for clients or timeout"
2020-05-20 13:17:58 +00:00
STATE=K
fi
fi
;;
*)
2020-05-23 14:15:10 +00:00
logAutopause "Error: invalid state: $STATE"
2020-05-20 13:17:58 +00:00
;;
esac
if [[ "$STATE" == "S" ]] ; then
# before rcon times out
sleep 2
else
sleep $AUTOPAUSE_PERIOD
fi
done