Minecraft – How to a Minecraft server be restarted daily and gracefully on an Ubuntu server

hostingminecraft-java-editionUbuntu

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?

Best Answer

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)