TransWikia.com

How can a Minecraft server be restarted daily and gracefully on an Ubuntu server?

Arqade Asked by BlandCorporation on February 4, 2021

I’ve got a (modded) Minecraft server running in a tmux session on an Ubuntu 18.04 server. What might be a good way to get it to shut down gracefully and then relaunch daily at, say, 05:00 in the morning?

So, in the tmux window, I am running a command like the following:

while true; do java -Xmx15G -Xms15G -jar forge-1.14.4-28.2.0.jar nogui; sleep 120; done

This launches the Minecraft server, and relaunches it after a couple of minutes if it crashes. But in order to optimise performance, I would like the Minecraft server to shut down at, say, 05:00 in the morning gracefully, as it would with the Minecraft server command /stop, and then to restart. How might this be done?

Is there some sleep command for the server that could, on launch, cause the server to /stop after 24 hours, and then my little loop script restarts it? Is there a way to tell the server to stop when it detects there are no users?

2 Answers

You can actually do this with a plugin! https://www.spigotmc.org/resources/1-8-1-15-ultimateautorestart-need-an-autorestart-plugin-grab-the-best-one-today.64414/ is what you are looking for! (It will stop your server at a scheduled time, then your startup command will restart it)

Answered by Samuurai on February 4, 2021

If you don't want your command line to start the forge program even if it's already running, you can add a check to see if forge is running to it.

As per the highest answer there, your code would look like:

while true; do 
    if pgrep -f "forge" > /dev/null
    then
        java -Xmx15G -Xms15G -jar forge-1.14.4-28.2.0.jar nogui &
    else 
        echo "Minecraft appears to already be running";
    fi
    sleep 120;
done

The & will run java asynchronously, allowing more command to run while the server is running. (Insert them in between fi and sleep). This can be useful to solve your second point:

For your second question, you may perhaps want to check out an rcon client program. This allows you to access the minecraft command console from within bash and handles all the protocol and security features of the game for you.

You will want to:

  1. Read the 'player name' string into a variable.rcon -u admin -p password list
  2. If the output from above is empty, execute rcon -u admin -p password stop to stop the server.

See below:

$output=rcon -u admin -p password list
if [ -z "$output" ]; then 
    rcon -u admin -p password stop 
fi 

One small thing about this:

If you immediately stop the server (run the above in a loop just like your current script) then the last player disconnecting might immediately cause a reset. This may not be what you want: if the last player disconnects due to their internet they may lose their current state. Perhaps it would be a good idea to add a bit of delay: require the check to succeed twice in a row instead.

Answered by aphid on February 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP