How do I automatically restart a Minecraft Spigot server in the event of a crash or /stop when using screen?

I use screen to run my minecraft spigot server on linux so that I can do other tasks at the same time on the same console. When using screen, all restart scripts described on the Internet and the Spigot wiki no longer worked. The "/restart" command never worked either. So how do I restart the server automatically after a short time if it crashes or I shut it down with "/stop" ingame?

1

2 Answers

Tutorial

A friend wrote a bash script to automatically restart a minecraft (spigot) server in the event of a crash or with the command "/stop" when using screen. There are several seconds to cancel the restart with Enter. In addition, the exit codes of the previous session are written to a file, which can be used to understand when and why a server has crashed or restarted.

You need two files:

  1. "start.sh"
#!/bin/sh
screen -d -m -S "mc_spigot_server" ./startserver.sh
  1. "startserver.sh"
#!/bin/bash
JAR=spigot-1.15.2.jar
MAXRAM=1024M
MINRAM=1024M
TIME=20
while [ true ]; do java -Xmx$MAXRAM -Xms$MINRAM -jar $JAR nogui if [[ ! -d "exit_codes" ]]; then mkdir "exit_codes"; fi if [[ ! -f "exit_codes/server_exit_codes.log" ]]; then touch "exit_codes/server_exit_codes.log"; fi echo "[$(date +"%d.%m.%Y %T")] ExitCode: $?" >> exit_codes/server_exit_codes.log echo "----- Press enter to prevent the server from restarting in $TIME seconds -----"; read -t $TIME input; if [ $? == 0 ]; then break; else echo "------------------- SERVER RESTARTS -------------------"; fi
done

You can change the start parameters by changing the variables:

JAR = server filename

MAXRAM = maximum RAM

MINRAM = minimum RAM

TIME = time in seconds until server restarts automatically

Execute the following in the directory:

chmod +x start.sh startserver.sh

Run your start up script:

./start.sh
  • To leave the minecraft screen press Ctrl + A + D

  • To reconnect to minecraft screen use screen -r

Did you discover any mistakes or do you disagree? Help me do it better.

I have a problem with this script since I changed os from Linux Centos 8 to Ubuntu 18.

When I was using this script to restart my Minecraft server in Linux Centos 8 I had no problem; since I changed OS to Ubuntu, I can't stop the auto restart script.

I just translated in Italian a few text rows.

This is what I see in the screen during the auto restart:

console screenshot

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like