From fb7ea1517da73501a6da96f1e4504a6bed6f55b1 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Sun, 5 Jan 2020 01:18:24 +0100 Subject: added unit file --- Readme.md | 8 ++++++++ minecraft.service | 16 ++++++++++++++++ server.sh | 55 +++++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 minecraft.service diff --git a/Readme.md b/Readme.md index 29a3792..6ab827e 100644 --- a/Readme.md +++ b/Readme.md @@ -41,6 +41,14 @@ The following example copies the created archive to a remote server. BACKUP_HOOK="scp $ARCHNAME user@server:/home/user/backups/" +## Start automatically + +Create user and group `minecraft` with home in `/var/minecraft`. +Populate the directory with server.sh and a server jar. +Place `minecraft.service` in `/etc/systemd/system/` +and run `systemctl start minecraft` to start once or +`systemctl enable minecraft` to enable autostarting. + ## Disclaimer The scripts are provided as-is at no warranty. diff --git a/minecraft.service b/minecraft.service new file mode 100644 index 0000000..f261625 --- /dev/null +++ b/minecraft.service @@ -0,0 +1,16 @@ +# minecraft.service + +[Unit] +Description=Minecraft Server + +[Service] +Type=forking +User=minecraft +Group=minecraft +PIDFile=/var/minecraft/server-screen.pid +ExecStart=/var/minecraft/server.sh start +TimeoutStopSec= 180 +ExecStop=/var/minecraft/server.sh stop + +[Install] +WantedBy=multi-user.target diff --git a/server.sh b/server.sh index 5c4129c..9b8156b 100755 --- a/server.sh +++ b/server.sh @@ -7,8 +7,8 @@ JAR="server.jar" JAR_ARGS="-nogui" SCREEN_WINDOW="minecraftserverscreen" WORLD_NAME="world" -LOGFILE="mcserver.log" - +LOGFILE="server-screen.log" +PIDFILE="server-screen.pid" #HOOKS BACKUP_HOOK='backup_hook_example' @@ -20,14 +20,36 @@ function send_cmd () { screen -S $SCREEN_WINDOW -p 0 -X stuff "$1^M" } -function server_start() { - screen -list $SCREEN_WINDOW > /dev/null +function assert_running() { + if [ ! -e $PIDFILE ]; then + return + fi + + ps -p $(cat $PIDFILE) > /dev/null if [ $? -eq 0 ] then echo "It seems a server is already running. If this is not the case,\ manually attach to the running screen and close it." exit 1 fi +} + +function assert_not_running() { + if [ ! -e $PIDFILE ]; then + echo "Server not running" + exit 1 + fi + + ps -p $(cat $PIDFILE) > /dev/null + if [ ! $? -eq 0 ] + then + echo "Server not running" + exit 1 + fi +} + +function server_start() { + assert_running if [ ! -e "eula.txt" ] then @@ -36,27 +58,44 @@ function server_start() { fi rm -f $LOGFILE - screen -L -Logfile "$LOGFILE" -S $SCREEN_WINDOW -p 0 -d -m \ - $JRE_JAVA $JVM_ARGS -jar $JAR $JAR_ARGS + screen -L -Logfile "$LOGFILE" -S $SCREEN_WINDOW -p 0 -D -m \ + $JRE_JAVA $JVM_ARGS -jar $JAR $JAR_ARGS > /dev/null & + echo $! > $PIDFILE + echo Started with PID $! exit } function server_stop() { + assert_not_running send_cmd "stop" + + local RET=1 + while [ ! $RET -eq 0 ] + do + sleep 1 + ps -p $(cat $PIDFILE) > /dev/null + RET=$? + done + + echo "stopped the server" + + rm -f $PIDFILE + exit } function server_attach() { + assert_not_running screen -r -p 0 $SCREEN_WINDOW exit } function server_status() { - screen -list $SCREEN_WINDOW > /dev/null + ps -p $(cat $PIDFILE) > /dev/null if [ $? -eq 0 ] then - echo "Server seems to be running. attach to be sure" + echo "Server is running" else echo "Server is not running" fi -- cgit v1.2.3