Add more complex stop-start behavior

This commit is contained in:
Salt 2021-01-24 20:27:17 -06:00
parent d325f47cd9
commit dbc9e3f80f
4 changed files with 57 additions and 8 deletions

View File

@ -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"

View File

@ -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

10
templates/start-server.sh Normal file
View File

@ -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

39
templates/stop-server.sh Normal file
View File

@ -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