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?

1 Answer 1


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
        java -Xmx15G -Xms15G -jar forge-1.14.4-28.2.0.jar nogui &
        echo "Minecraft appears to already be running";
    sleep 120;

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 

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .