LochStudios  /  Help Centre  /  Game Servers  /  Keep a Game Server Running and Auto-Restarting with systemd

Keep a Game Server Running and Auto-Restarting with systemd

Use systemd service files to keep your game server running automatically and restart it if it crashes.

Updated

Running a game server on a Linux VPS? systemd is the standard way to manage processes, keep them running after reboots, and restart them if they crash. This guide walks you through creating a systemd service for any game server.

Step 1: Create a Dedicated User (Optional but Recommended)

For security, run your game server under its own user account rather than root:

sudo useradd -m -s /bin/bash gameserver

If your game server files are in a specific directory, give that user ownership:

sudo chown -R gameserver:gameserver /opt/gameserver

Step 2: Create the systemd Service File

Create a new service file in /etc/systemd/system/. Replace gameserver.service with a meaningful name (e.g., minecraft.service or csgo.service):

sudo nano /etc/systemd/system/gameserver.service

Paste this template and customize the values:

[Unit]
Description=Game Server
After=network.target

[Service]
Type=simple
User=gameserver
WorkingDirectory=/opt/gameserver
ExecStart=/opt/gameserver/start.sh
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target

Key fields to customize:
- Description: A human-readable name for your server.
- User: The user account to run the server under (use your username if not creating a dedicated user).
- WorkingDirectory: The directory where your server files live.
- ExecStart: The command that starts your server (e.g., /opt/gameserver/start.sh or /usr/bin/java -Xmx4G -jar server.jar nogui).
- RestartSec: Seconds to wait before restarting after a crash (10 is a safe default).

Step 3: Create the Start Script

If you used a script like /opt/gameserver/start.sh in ExecStart, create it:

sudo nano /opt/gameserver/start.sh

Example for a Java-based server (Minecraft, etc.):

#!/bin/bash
cd /opt/gameserver
exec /usr/bin/java -Xmx4G -Xms2G -jar server.jar nogui

For a compiled binary server, the script might just be:

#!/bin/bash
cd /opt/gameserver
exec ./gameserver

Make the script executable:

sudo chmod +x /opt/gameserver/start.sh

Step 4: Reload systemd and Start the Service

Tell systemd to recognize your new service:

sudo systemctl daemon-reload

Start the service:

sudo systemctl start gameserver

Enable it to auto-start on boot:

sudo systemctl enable gameserver

Step 5: Verify the Service is Running

Check the status:

sudo systemctl status gameserver

You should see active (running). View live logs:

sudo journalctl -u gameserver -f

Press Ctrl+C to exit the log view.

Troubleshooting

Service fails to start? Check the logs:

sudo journalctl -u gameserver -n 50

Server crashes repeatedly? Increase RestartSec to give it more time between restarts, or check your server logs for errors.

Need to restart manually?

sudo systemctl restart gameserver

Stop the server:

sudo systemctl stop gameserver

Disable auto-start on boot:

sudo systemctl disable gameserver

Was this article helpful?

← Back to Game Servers