diff --git a/files/autopause/autopause-daemon.sh b/files/autopause/autopause-daemon.sh index b4896d43..3150740c 100644 --- a/files/autopause/autopause-daemon.sh +++ b/files/autopause/autopause-daemon.sh @@ -1,9 +1,9 @@ #!/bin/bash -exec 1>/tmp/terminal-mc - . /autopause/autopause-fcns.sh +. /start-utils + sudo /usr/sbin/knockd -c /autopause/knockd-config.cfg -d if [ $? -ne 0 ] ; then while : @@ -13,10 +13,10 @@ if [ $? -ne 0 ] ; then fi sleep 0.1 done - echo "[Autopause loop] Failed to start knockd daemon." - echo "[Autopause loop] Possible cause: docker's host network mode." - echo "[Autopause loop] Recreate without host mode or disable autopause functionality." - echo "[Autopause loop] Stopping server." + 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." killall -SIGTERM java exit 1 fi @@ -30,18 +30,18 @@ do # Server startup if mc_server_listening ; then TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_INIT)) - echo "[Autopause loop] MC Server listening for connections - stopping in $AUTOPAUSE_TIMEOUT_INIT seconds" + logAutopause "MC Server listening for connections - stopping in $AUTOPAUSE_TIMEOUT_INIT seconds" STATE=K fi ;; XK) # Knocked if java_clients_connected ; then - echo "[Autopause loop] Client connected - waiting for disconnect" + logAutopause "Client connected - waiting for disconnect" STATE=E else if [[ $(current_uptime) -ge $TIME_THRESH ]] ; then - echo "[Autopause loop] No client connected since startup / knocked - stopping" + logAutopause "No client connected since startup / knocked - stopping" /autopause/pause.sh STATE=S fi @@ -51,18 +51,18 @@ do # Established if ! java_clients_connected ; then TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_EST)) - echo "[Autopause loop] All clients disconnected - stopping in $AUTOPAUSE_TIMEOUT_EST seconds" + logAutopause "All clients disconnected - stopping in $AUTOPAUSE_TIMEOUT_EST seconds" STATE=I fi ;; XI) # Idle if java_clients_connected ; then - echo "[Autopause loop] Client reconnected - waiting for disconnect" + logAutopause "Client reconnected - waiting for disconnect" STATE=E else if [[ $(current_uptime) -ge $TIME_THRESH ]] ; then - echo "[Autopause loop] No client reconnected - stopping" + logAutopause "No client reconnected - stopping" /autopause/pause.sh STATE=S fi @@ -75,17 +75,17 @@ do fi if java_running ; then if java_clients_connected ; then - echo "[Autopause loop] Client connected - waiting for disconnect" + logAutopause "Client connected - waiting for disconnect" STATE=E else TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_KN)) - echo "[Autopause loop] Server was knocked - waiting for clients or timeout" + logAutopause "Server was knocked - waiting for clients or timeout" STATE=K fi fi ;; *) - echo "[Autopause loop] Error: invalid state: $STATE" + logAutopause "Error: invalid state: $STATE" ;; esac if [[ "$STATE" == "S" ]] ; then diff --git a/files/autopause/pause.sh b/files/autopause/pause.sh index 3acc5bd6..c84bdda5 100755 --- a/files/autopause/pause.sh +++ b/files/autopause/pause.sh @@ -1,5 +1,7 @@ #!/bin/bash +. /start-utils + if [[ $( ps -a -o stat,comm | grep 'java' | awk '{ print $1 }') =~ ^S.*$ ]] ; then # save world rcon-cli save-all >/dev/null @@ -14,6 +16,6 @@ if [[ $( ps -a -o stat,comm | grep 'java' | awk '{ print $1 }') =~ ^S.*$ ]] ; th done # finally pause the process - echo "[$(date -Iseconds)] [Autopause] Pausing Java process" >/tmp/terminal-mc + logAutopauseAction "Pausing Java process" killall -q -STOP java fi diff --git a/files/autopause/resume.sh b/files/autopause/resume.sh index dcf0a1dd..73ab648e 100755 --- a/files/autopause/resume.sh +++ b/files/autopause/resume.sh @@ -1,6 +1,8 @@ #!/bin/bash +. /start-utils + if [[ $( ps -a -o stat,comm | grep 'java' | awk '{ print $1 }') =~ ^T.*$ ]] ; then - echo "[$(date -Iseconds)] [Autopause] Knocked, resuming Java process" >/tmp/terminal-mc + logAutopauseAction "Knocked, resuming Java process" killall -q -CONT java fi diff --git a/start b/start index 37c422c2..c50aef4f 100644 --- a/start +++ b/start @@ -41,9 +41,6 @@ if [ $(id -u) = 0 ]; then chown -R ${runAsUser}:${runAsGroup} /autopause fi - ln -fs $(tty) /tmp/terminal-mc - chmod 777 /tmp/terminal-mc - if [[ ${SKIP_NSSWITCH_CONF^^} != TRUE ]]; then echo 'hosts: files dns' > /etc/nsswitch.conf fi diff --git a/start-utils b/start-utils index 03b4c562..e054396a 100644 --- a/start-utils +++ b/start-utils @@ -49,6 +49,14 @@ function log { echo "[init] $*" } +function logAutopause { + echo "[Autopause loop] $*" +} + +function logAutopauseAction { + echo "[$(date -Iseconds)] [Autopause] $*" +} + function normalizeMemSize { local scale=1 case ${1,,} in