From dbc9e3f80f793f03170ef7b17dd0ca99435d2563 Mon Sep 17 00:00:00 2001 From: Salt Date: Sun, 24 Jan 2021 20:27:17 -0600 Subject: [PATCH] Add more complex stop-start behavior --- tasks/main.yml | 6 ++++++ templates/minecraft@.service | 10 ++------- templates/start-server.sh | 10 +++++++++ templates/stop-server.sh | 39 ++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 templates/start-server.sh create mode 100644 templates/stop-server.sh diff --git a/tasks/main.yml b/tasks/main.yml index 3dcc2cb..13d0538 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -20,6 +20,12 @@ template: src=eula.txt dest={{ minecraft_home }}/{{ minecraft_name }}/eula.txt - name: template out env template: src=env dest={{ minecraft_home }}/{{ minecraft_name }}/env + - name: template start script + template: src=start-server.sh dest={{ minecraft_home }}/{{ minecraft_name }}/start-server.sh mode="0744" + notify: "restart minecraft {{ minecraft_name }}" + - name: template stop script + template: src=stop-server.sh dest={{ minecraft_home }}/{{ minecraft_name }}/stop-server.sh mode="0744" + notify: "restart minecraft {{ minecraft_name }}" - name: configure server properties lineinfile: dest: "{{ minecraft_home }}/{{ minecraft_name }}/server.properties" diff --git a/templates/minecraft@.service b/templates/minecraft@.service index 666d9f2..d271506 100644 --- a/templates/minecraft@.service +++ b/templates/minecraft@.service @@ -23,14 +23,8 @@ ProtectKernelModules=true # Implies MountAPIVFS=yes ProtectControlGroups=true -ExecStart=/bin/sh -c '/usr/bin/screen -DmS %i /usr/bin/java -Xms${JRE_XMS} -Xmx${JRE_XMX} -jar server.jar nogui' - -ExecReload=/usr/bin/screen -p 0 -S %i -X eval 'stuff "reload"\\015' - -ExecStop=/usr/bin/screen -p 0 -S %i -X eval 'stuff "say Server is going down: Service was stopped"\\015' -ExecStop=/usr/bin/screen -p 0 -S %i -X eval 'stuff "save-all"\\015' -ExecStop=/usr/bin/screen -p 0 -S %i -X eval 'stuff "stop"\\015' -ExecStop=/bin/sleep ${MC_RESTART_DELAY} +ExecStart={{ minecraft_home }}/%i/start-server.sh +ExecStop={{ minecraft_home }}/%i/stop-server.sh Restart=always # Restart after 24hrs diff --git a/templates/start-server.sh b/templates/start-server.sh new file mode 100644 index 0000000..176e529 --- /dev/null +++ b/templates/start-server.sh @@ -0,0 +1,10 @@ +#! /bin/bash +instance="{{ minecraft_name }}" + +/usr/bin/screen -DmS "$instance" \ + /usr/bin/java \ + -Xms"$JRE_XMS" \ + -Xmx"$JRE_XMX" \ + -Dfml.queryResult=confirm \ + -jar server.jar \ + nogui diff --git a/templates/stop-server.sh b/templates/stop-server.sh new file mode 100644 index 0000000..65d3053 --- /dev/null +++ b/templates/stop-server.sh @@ -0,0 +1,39 @@ +#! /bin/bash +# vim:ft=bash +instance="{{ minecraft_name }}" +screen="/usr/bin/screen -p 0 -S $instance" +stuff() { + [ -z "$1" ] && return 1 + $screen -X eval "stuff \"$1\"\\015" +} +# Junk FD used for read waiting +exec 1023<> <(:) + +stuff "say Server is going down for maintenance!" + +if [ -n "$MC_RESTART_DELAY" ] && [ "$MC_RESTART_DELAY" -gt 0 ]; then + for (( i="$MC_RESTART_DELAY"; i>0; i-- )); do + case "$i" in + 300) + stuff "say Server is going down in 5 minutes!" + ;; + 60) + stuff "say Server is going down in 1 minute!" + ;; + 30|10|5|4|3|2) + stuff "say Server is going down in "$i" seconds!" + ;; + 1) + stuff "say Server is going down NOW!" + ;; + *) + ;; + esac + read -t 1 -u 1023 || : + done +fi + +stuff "save-all" +read -t 10 -u 1023 || : +stuff "stop" +exit 0