Autostart Minecraft Server

Hi how can make this sh script runs after boot? I tried this but it doesn't work.

/etc/systemd/system/my-startup.service

[Unit]
Description=Start Minecraft
After=network.target

[Service]
Type=simple
ExecStart=/home/dre/minecraft/start.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target

/home/dre/minecraft/start.sh

#!/bin/bash
screen -d -m java -Xmx3990M -Xms3990M -jar server.jar nogui

systemctl status my-startup.sh

● my-startup.service - Start Minecraft
     Loaded: loaded (/etc/systemd/system/my-startup.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Mon 2021-01-11 00:50:09 UTC; 10min ago
   Main PID: 625 (code=exited, status=0/SUCCESS)

Jan 11 00:50:09 optiplex systemd[1]: Started Start Minecraft.
Jan 11 00:50:09 optiplex systemd[1]: my-startup.service: Succeeded.

But there's nothing in screen -ls and server hasn't turned on.

1 Answer

There are problems with your .service file.

  1. You are running the script as root. Bad Idea. You should run as a non-root user, in case your server gets compromised. Add a User=<username> line to the [Service] section.

  2. Use the .service file to trigger screen directly using the ExecStart= line. That way you can use systemd the way it's intended as a tool to start, stop, restart, and monitor the Minecraft server.

Here's an example from my own Minecraft server....

[Service]
RemainAfterExit=yes
WorkingDirectory=/home/minecraft
User=minecraft
# Start Screen, Java, and Minecraft
ExecStart=screen -s mc -d -m java -server -Xms512M -Xmx1024M -jar server.jar nogui
# Tell Minecraft to gracefully stop.
# Ending Minecraft will terminate Java
# systemd will kill Screen after the 10-second delay. No explicit kill for Screen needed
ExecStop=screen -p 0 -S mc -X eval 'stuff "say SERVER SHUTTING DOWN. Saving map..."\015'
ExecStop=screen -p 0 -S mc -X eval 'stuff "save-all"\015'
ExecStop=screen -p 0 -S mc -X eval 'stuff "stop"\015'
ExecStop=sleep 10
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest