This simple script takes care of your minecraft server restarts. It also creates backup of your world by zipping world, world_nether and world_the_end directories and putting them in backup folder. What's more, it deletes the backup files if they are older than 7 days.
- Put server.sh in your server directory.
- Open server.sh and edit Settings section. Save and close the file.
- Use
sudo chmod +x server.sh
to allow the script to be executed. - Make sure you have zip and tmux installed. (You can use also use screen but I like tmux better). If not, use
sudo apt install zip
andsudo apt install tmux
. - Type
mkdir backup
. This will create new directory called backup. All world backups will be stored there. - Type
crontab -e
. This will bring up text editor with your cronjob tasks. At the end of the file add the line that will stop (restart) your server. The command istmux send -t minecraft "stop" Enter
. You can use sites like https://crontab-generator.org/ to generate cronjob tasks for you. Your file should look similar to this:
# This line stops (and restarts) the server every day at midnight.
0 0 * * * tmux send -t minecraft "stop" Enter
# This line sends a warning 5 minutes before server restart
55 23 * * * tmux send -t minecraft "say Server restart in 5 minutes..." Enter
- Save the file. You are now ready to use the script.
- Create new tmux session by executing
tmux new -s minecraft
. - Now run the script by entering
./server.sh
. You should see the server beggining to load. - Press Ctrl + B. Then press D to detach from session.
That's it. Now everytime your server goes down by /stop
command or by some error, it will be automatically restarted and a new backup file will be created.
If you want to attach to the session again to execute some minecraft commands, type tmux attach -t minecraft
. If you want to kill the server completely, just execute tmux kill-session -t minecraft
.