Start a Minecraft server automatically: systemd service and cron job
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,restartandstatus. - 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/serverin 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:
AfterandWantsmake sure that the server only starts once the network is available.Userdefines which user the server runs as. Never operate it as root.WorkingDirectoryis the directory with the server file. Without this setting, Java will not find the JAR file.ExecStopsends the "stop" command to the screen session. That way the server saves the world properly instead of simply being killed.Restart=on-failurerestarts the server automatically after a crash,RestartSecsets 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
-dmthe 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
-Xmxis 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?
Why does screen -S have to be changed to screen -dmS?
How do I reach the server console after an automatic start?
Does systemd also restart my server after a crash?
How do I check whether the autostart really works?
2024-2026 KernelHost GmbH. All rights reserved. This guide is protected by copyright. Republishing it on other websites, in whole, in part or in edited form, is not permitted without our written consent. Quoting with a source credit and a link is expressly welcome.

