How to Set Up Game Server Auto-Restart with systemd
One of the most important aspects of running a game server on your Breeze instance is ensuring it stays online. Using systemd service units, you can automatically start your game server on boot, restart it if it crashes, and manage it cleanly with standard Linux tools. This guide covers creating robust systemd services for any game server.
Why Use systemd
Many guides suggest using screen or tmux to keep game servers running, but systemd offers significant advantages:
- Auto-start on boot — your server comes back automatically after a Breeze reboot
- Crash recovery — systemd restarts the server automatically when it crashes
- Clean shutdown — proper signal handling ensures saves are written before stopping
- Logging — integrated with journalctl for centralized log management
- Resource control — cgroups integration for CPU and memory limits
Creating a Basic Service
Create a systemd service file at /etc/systemd/system/gameserver.service:
[Unit]
Description=Game Server
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=gameserver
Group=gameserver
WorkingDirectory=/home/gameserver/server
ExecStart=/home/gameserver/server/start.sh
ExecStop=/bin/kill -SIGINT $MAINPID
Restart=on-failure
RestartSec=30
StartLimitIntervalSec=300
StartLimitBurst=5
[Install]
WantedBy=multi-user.target
Understanding the Options
Key configuration options explained:
- Restart=on-failure — restarts the service when it exits with a non-zero code or is killed by a signal
- RestartSec=30 — waits 30 seconds before restarting to avoid rapid restart loops
- StartLimitIntervalSec/Burst — limits restarts to 5 within 300 seconds to prevent infinite crash loops
- ExecStop with SIGINT — sends an interrupt signal allowing the server to save before shutting down
Example: Minecraft Server
A complete Minecraft server service:
[Unit]
Description=Minecraft Server
After=network-online.target
[Service]
Type=simple
User=minecraft
WorkingDirectory=/home/minecraft/server
ExecStart=/usr/bin/java -Xmx4G -Xms2G -jar server.jar nogui
ExecStop=/bin/kill -SIGTERM $MAINPID
TimeoutStopSec=60
Restart=on-failure
RestartSec=15
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
Example: Source Engine Server
For Source engine games like Left 4 Dead 2, Counter-Strike, or Garry’s Mod:
[Unit]
Description=Source Engine Game Server
After=network-online.target
[Service]
Type=simple
User=srcds
WorkingDirectory=/home/srcds/server
ExecStart=/home/srcds/server/srcds_run -game garrysmod \
+maxplayers 24 +map gm_flatgrass +port 27015
Restart=always
RestartSec=10
KillSignal=SIGTERM
TimeoutStopSec=30
[Install]
WantedBy=multi-user.target
Enabling and Managing the Service
After creating the service file, enable and start it:
sudo systemctl daemon-reload
sudo systemctl enable gameserver.service
sudo systemctl start gameserver.service
Manage the server with standard systemctl commands:
sudo systemctl status gameserver # Check status
sudo systemctl stop gameserver # Stop the server
sudo systemctl restart gameserver # Restart the server
journalctl -u gameserver -f # Follow live logs
Adding Resource Limits
Use cgroups via systemd to prevent a game server from consuming all resources on your Breeze:
[Service]
MemoryMax=6G
MemoryHigh=5G
CPUQuota=200%
TasksMax=512
CPUQuota=200% limits the service to 2 CPU cores. MemoryMax sets a hard memory ceiling while MemoryHigh triggers memory pressure reclamation.
Scheduled Restarts with Timers
Create a timer unit for periodic restarts. Create /etc/systemd/system/gameserver-restart.timer:
[Unit]
Description=Restart Game Server Every 6 Hours
[Timer]
Persistent=true
[Install]
WantedBy=timers.target
And a corresponding gameserver-restart.service:
[Unit]
Description=Restart Game Server
[Service]
Type=oneshot
ExecStart=/bin/systemctl restart gameserver.service
Enable the timer:
sudo systemctl enable --now gameserver-restart.timer
Pre-Shutdown Save Script
For servers that support RCON, create a wrapper script that sends a save command before shutdown:
#!/bin/bash
# /home/gameserver/graceful-stop.sh
mcrcon -H localhost -P 25575 -p rcon_password "say Server restarting in 30 seconds..."
sleep 25
mcrcon -H localhost -P 25575 -p rcon_password "say Server restarting in 5 seconds..."
sleep 5
mcrcon -H localhost -P 25575 -p rcon_password "save-all"
sleep 3
mcrcon -H localhost -P 25575 -p rcon_password "stop"
Reference this script with ExecStop= in your service file instead of a raw kill signal for the cleanest shutdowns on your Breeze instance.