Start a Minecraft server automatically: systemd service and cron job

Published on Updated on 5 min read

After a server reboot your Minecraft server should come back up on its own. We show the systemd service unit as the clean way and the cron job with @reboot as the alternative.

Should your Minecraft server come back online by itself after a reboot of your root server, without you having to log in over SSH? Then you are in the right place.

We will show you two approaches: the systemd service unit as the clean and recommended solution, and the classic cron job with @reboot as a quick alternative. Both variants work on all common Linux distributions, so on Debian, Ubuntu, AlmaLinux and Rocky Linux.

systemd or cron job: which approach is the right one?

A cron job with @reboot is set up quickly, but it only knows a single point in time: system start. If something goes wrong there, or if the server crashes later on, nothing else happens.

A systemd service unit is the clean way and brings several advantages with it:

  • The server only starts once the network is really ready, instead of after a guessed waiting time.
  • After a crash, systemd restarts the server automatically.
  • You operate the server with the familiar commands systemctl start, stop, restart and status.
  • Error messages end up in the journal and can be traced with journalctl.

That is why we recommend the systemd route. We describe the cron job further down as an alternative for cases in which systemd is not available.

Preparation: note down the start command, user and directory

Before you get started, you need three details from your existing installation:

  • The start command from your "start.sh" file, for example screen -dmS spigot java -Xmx4G -jar spigot.jar
  • The user that the server runs as, "spigot" in this example
  • The directory that holds the server file, /home/spigot/server in this example

A typical start.sh has content like this:

#This is only an example! Use the contents of your own start.sh.

screen -S spigot java -Xmx2G -jar spigot.jar

Important: for the automatic start, screen -S absolutely has to become screen -dmS. The reason: screen -S opens an interactive session and expects a terminal. At system start there is no terminal, so the start would fail. screen -dmS, in contrast, starts the session directly in the background.

If you have not set up a Minecraft server at all yet, our guide Installing a Minecraft server on Debian will help you first.

Creating a systemd service unit for the Minecraft server

Create the unit file

As root, create a new service file:

nano /etc/systemd/system/minecraft.service

Enter the following content and adjust the user, directory, RAM and file name to match your installation:

[Unit]
Description=Minecraft-Server
After=network-online.target
Wants=network-online.target

[Service]
Type=forking
User=spigot
WorkingDirectory=/home/spigot/server
ExecStart=/usr/bin/screen -dmS spigot java -Xmx4G -jar spigot.jar
ExecStop=/usr/bin/screen -S spigot -X stuff "stop\n"
Restart=on-failure
RestartSec=15

[Install]
WantedBy=multi-user.target

The most important lines at a glance:

  • After and Wants make sure that the server only starts once the network is available.
  • User defines which user the server runs as. Never operate it as root.
  • WorkingDirectory is the directory with the server file. Without this setting, Java will not find the JAR file.
  • ExecStop sends the "stop" command to the screen session. That way the server saves the world properly instead of simply being killed.
  • Restart=on-failure restarts the server automatically after a crash, RestartSec sets the waiting time in seconds.

Save the file with "CTRL + X", then "Y" and "Enter".

Enable and start the service

After that, systemd reads the new file, enables the autostart and starts the server immediately:

systemctl daemon-reload
systemctl enable --now minecraft

You check the current state with:

systemctl status minecraft

If the output shows "active", the service is running. If there are problems, the journal delivers the exact error message:

journalctl -u minecraft -n 50

Reaching the server console

The server still runs inside a screen session. Since that session belongs to the user "spigot", switch to that user first and then open the console:

su spigot
screen -r spigot

With "CTRL + A + D" you leave the console again without stopping the server.

Stopping and restarting the server

From now on you control the server through systemd:

systemctl stop minecraft
systemctl restart minecraft

If the autostart should be disabled again, this is enough:

systemctl disable minecraft

Alternative: autostart through a cron job with @reboot

If you would rather not create a service unit, you can also achieve the autostart through the crontab of the respective user. Open the crontab with:

export VISUAL=nano; crontab -e

Add your start command as the last line, with the prefix @reboot:

@reboot <contents of your start.sh file>

#This is roughly what the start parameter should look like, it may differ from your own start script.

@reboot screen -dmS <screen-name> java -Xmx<AMOUNT>G -jar <directory and name of the Minecraft server file>.jar


#EXAMPLE:
@reboot screen -dmS spigot java -Xmx2G -jar /home/spigot/spigot.jar

Here as well, make sure that screen -dmS is used and not screen -S. Our guide Running programs automatically at server start shows how to start any other applications and scripts the same way when the server boots.

Testing the autostart

Do not simply rely on the configuration being correct, test it once on purpose. Reboot the server:

reboot

Wait one or two minutes, log in over SSH again and check with systemctl status minecraft or with screen -ls whether the Minecraft server is running again. Also connect with the game client so that you are sure the server really is reachable.

Common mistakes with the autostart

  • screen -S instead of screen -dmS: by far the most common mistake. Without -dm the server does not start at boot.
  • Wrong directory: Java does not find the JAR file. Check WorkingDirectory, or give the full path to the JAR file in the cron job.
  • Wrong user: if the service runs as root, the newly created world files belong to root afterwards, and the actual server user can no longer write to them.
  • Too little RAM: if the value behind -Xmx is larger than the free RAM, the kernel terminates the Java process again shortly after the start.

Frequently asked questions

systemd or cron job: which is the better approach?
A systemd service unit is the cleaner solution. It starts the server only once the network is ready, restarts it automatically after a crash and can be operated with systemctl start, stop and status. A cron job with @reboot offers none of these features.
Why does screen -S have to be changed to screen -dmS?
screen -S opens an interactive session and expects a terminal. At system start there is no terminal, so the start fails. screen -dmS starts the session in the background and is therefore the correct variant for the autostart.
How do I reach the server console after an automatic start?
First log in as the user that the service runs as, then open the session with screen -r. A screen session always belongs only to the user who started it.
Does systemd also restart my server after a crash?
Yes, as long as you set Restart=on-failure in the unit file. systemd then starts the server again after the waiting time specified under RestartSec.
How do I check whether the autostart really works?
Deliberately reboot the server once with reboot and then check with systemctl status minecraft whether the service is running. If there are errors, journalctl -u minecraft shows the exact message.

Minecraft Server Autostart systemd Cron Jobs Crontabs Linux Game Server Debian