انتقل إلى المحتوى الرئيسي
العودة إلى الفئة

التشغيل التلقائي لخادم الألعاب: systemd و Watchdog

تشغيل خادم الألعاب تلقائياً مع systemd، سكريبتات watchdog، سياسات إعادة التشغيل واستخدام tmux/screen.

وقت القراءة: 12 dk إعداد خادم الألعاب
systemdwatchdogتشغيل تلقائيtmuxscreenخادم ألعابإعادة تشغيل

جدول المحتويات

Introduction

Game servers are services that need to run 24/7. Automatic restart in case of server crashes, system reboots, or unexpected errors is critical. This guide covers systemd service configuration, watchdog scripts, tmux/screen usage, and restart policy settings in detail.

Creating a systemd Service

Basic Service File

hljs bash
sudo nano /etc/systemd/system/gameserver.service
hljs ini
[Unit]
Description=Game Dedicated Server
After=network.target
Wants=network-online.target

[Service]
Type=simple
User=steam
Group=steam
WorkingDirectory=/home/steam/gameserver
ExecStartPre=/home/steam/steamcmd/steamcmd.sh +force_install_dir /home/steam/gameserver +login anonymous +app_update APP_ID +quit
ExecStart=/home/steam/gameserver/start.sh
ExecStop=/bin/kill -SIGINT $MAINPID
Restart=on-failure
RestartSec=30
StartLimitIntervalSec=300
StartLimitBurst=5
TimeoutStartSec=180
TimeoutStopSec=60
StandardOutput=journal
StandardError=journal
SyslogIdentifier=gameserver
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target

Service Management

hljs bash
# Enable service (auto-start on boot)
sudo systemctl daemon-reload
sudo systemctl enable gameserver

# Service management
sudo systemctl start gameserver
sudo systemctl stop gameserver
sudo systemctl restart gameserver
sudo systemctl status gameserver

# View logs
sudo journalctl -u gameserver -f
sudo journalctl -u gameserver --since "1 hour ago"

Restart Policy Settings

Restart Options

OptionDescription
noNo restart
on-successOnly on successful exit
on-failureOn error (exit code != 0)
on-abnormalOn signal or timeout
on-watchdogOn watchdog timeout
alwaysAlways restart
hljs ini
[Service]
Restart=always
RestartSec=15
StartLimitIntervalSec=600
StartLimitBurst=10

This configuration:

  • Server always restarts
  • Waits 15 seconds between restarts
  • Maximum 10 restarts within 600 seconds
  • Service enters failed state if limit exceeded

Resetting Failed Service

hljs bash
sudo systemctl reset-failed gameserver
sudo systemctl start gameserver

Watchdog Script

Simple Watchdog

hljs bash
nano ~/watchdog.sh
hljs bash
#!/bin/bash
SERVICE="gameserver"
LOG="/home/steam/watchdog.log"

while true; do
    if ! systemctl is-active --quiet $SERVICE; then
        echo "$(date): $SERVICE not running, restarting..." >> $LOG
        sudo systemctl restart $SERVICE
        sleep 30
    fi
    sleep 60
done
hljs bash
chmod +x ~/watchdog.sh

Advanced Watchdog (Port Check)

hljs bash
nano ~/watchdog_advanced.sh
hljs bash
#!/bin/bash
SERVICE="gameserver"
PORT=27015
LOG="/home/steam/watchdog.log"
MAX_RETRIES=3
RETRY_COUNT=0

check_port() {
    ss -tuln | grep -q ":$PORT "
    return $?
}

check_process() {
    systemctl is-active --quiet $SERVICE
    return $?
}

while true; do
    if ! check_process; then
        echo "$(date): Process not running, restarting..." >> $LOG
        sudo systemctl restart $SERVICE
        RETRY_COUNT=$((RETRY_COUNT + 1))
        sleep 60
    elif ! check_port; then
        echo "$(date): Port $PORT not responding, checking..." >> $LOG
        RETRY_COUNT=$((RETRY_COUNT + 1))
        if [ $RETRY_COUNT -ge $MAX_RETRIES ]; then
            echo "$(date): $MAX_RETRIES attempts failed, restarting server..." >> $LOG
            sudo systemctl restart $SERVICE
            RETRY_COUNT=0
        fi
        sleep 30
    else
        RETRY_COUNT=0
    fi
    sleep 60
done
hljs bash
chmod +x ~/watchdog_advanced.sh

Running Watchdog with systemd

hljs bash
sudo nano /etc/systemd/system/gameserver-watchdog.service
hljs ini
[Unit]
Description=Game Server Watchdog
After=gameserver.service
Requires=gameserver.service

[Service]
Type=simple
User=steam
ExecStart=/home/steam/watchdog_advanced.sh
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
hljs bash
sudo systemctl daemon-reload
sudo systemctl enable gameserver-watchdog
sudo systemctl start gameserver-watchdog

Server Management with tmux

tmux Installation

hljs bash
sudo apt install tmux -y

Starting Server with tmux

hljs bash
nano ~/start_tmux.sh
hljs bash
#!/bin/bash
SESSION="gameserver"
tmux kill-session -t $SESSION 2>/dev/null
tmux new-session -d -s $SESSION
tmux send-keys -t $SESSION "cd ~/gameserver && ./start.sh" C-m
echo "Server started in tmux session '$SESSION'."
echo "To attach: tmux attach -t $SESSION"
hljs bash
chmod +x ~/start_tmux.sh

tmux Basic Commands

hljs bash
tmux attach -t gameserver      # Attach to session
# Ctrl+B, then D              # Detach from session
tmux list-sessions             # List sessions
tmux kill-session -t gameserver # Kill session

Server Management with screen

screen Installation

hljs bash
sudo apt install screen -y

Starting Server with screen

hljs bash
nano ~/start_screen.sh
hljs bash
#!/bin/bash
SCREEN_NAME="gameserver"
screen -S $SCREEN_NAME -X quit 2>/dev/null
screen -dmS $SCREEN_NAME bash -c "cd ~/gameserver && ./start.sh"
echo "Server started in screen '$SCREEN_NAME'."
echo "To attach: screen -r $SCREEN_NAME"
hljs bash
chmod +x ~/start_screen.sh

screen Basic Commands

hljs bash
screen -r gameserver           # Attach to screen
# Ctrl+A, then D              # Detach from screen
screen -ls                     # List screens
screen -S gameserver -X quit   # Kill screen

Automatic Update and Restart

hljs bash
nano ~/auto_update_restart.sh
hljs bash
#!/bin/bash
SERVICE="gameserver"
LOG="/home/steam/update.log"
echo "$(date): Starting update..." >> $LOG
sudo systemctl stop $SERVICE
sleep 10
~/steamcmd/steamcmd.sh +force_install_dir ~/gameserver +login anonymous +app_update APP_ID +quit >> $LOG 2>&1
sudo systemctl start $SERVICE
echo "$(date): Update complete." >> $LOG
hljs bash
chmod +x ~/auto_update_restart.sh
crontab -e
0 4 * * * /home/steam/auto_update_restart.sh

Conclusion

With systemd service configuration, watchdog scripts, and tmux/screen usage, you can ensure your game server runs 24/7 without interruption. Proper restart policies and automatic update mechanisms automate server management. REXE VDS reliable infrastructure ensures your server is always accessible.