[Linux] Server auto-restart

[Linux] Server auto-restart

4.2k Downloads

These scripts are intended for use on Linux servers, however can be used on the desktop version as well. Because it's intended for Linux servers, we will be using a program called "Screen" to background our Minecraft server. For this example, I will be using Spigot as my server, but this would work with any Minecraft edition [Bukkit, Vanilla, Spigot, Tekkit...].

Instructions:

1. Open your terminal (via ssh,  or otherwise).

2. Check to see if "Screen" is already installed.

which screen

3. If a path is returned, example: "/usr/bin/screen", then Screen is installed. Skip to #5.

4. Install Screen by typing

Ubuntu:
sudo apt-get update sudo apt-get install screen
CentOS, RHEL:
yum install screen

5. Now that Screen is installed, download the two .sh files I've provided, and place them in your Minecraft's working directory. For this example, I will be using "/root/minecraft/" as my working directory. (This should be the same place you have your Minecraft servername.jar file stored. Example:

root folder

7. Now, edit them using the "vi" editor by typing

vi MCServerResetCheck.sh

8. Then edit the lines, highlighted below, to reflect your file path. You may also choose to change the "MCServerRestartLog.txt" to a different file name:

replace these lines

9. Save this file by typing "[ESC] :wq", which writes and quits the vi editor. Now, do the same with "start.sh". This time, only changing the lines highlighted below to reflect the startup of your server.jar file. In the image below I use the string "java -Xmx1g -Xms1g -jar spigot-1.11.2.jar".

change java startup

10. Now that we've configured all of this, we're going to set up a cronjob to execute every 30 seconds, making sure your server will restart itself if it crashes. Do this by typing:

service cron stop
crontab -e

11. This is your crontab. In your crontab file, enter these lines (change the path to your working directory):

* * * * * /root/minecraft/MCServerResetCheck.sh
* * * * * ( sleep 30 ; /root/minecraft/MCServerResetCheck.sh)

12. If you wish to not run this test so often (it's not CPU intensive, I promise), you can do so by removing one of the two lines above from your code, making the cronjob run once every minute. If you wish it to run every two minutes, change the asterisks from "* * * * *" to "*/2 * * * *". If you wish to have it run every three minutes, change it to "*/3 * * * *". This process can be repeated up to "*/59". For 59 minutes.

13. Now, restart cronjob by running:

service cron start

14. Yay! Your minecraft server should start itself automatically now! To make sure it's working, and to get back into the Minecraft console at any time, simply time "screen -r" to resume the screen session. To exit the screen session, type "[CTRL] + a" and then "d".