التشغيل التلقائي لخادم الألعاب: systemd و Watchdog
تشغيل خادم الألعاب تلقائياً مع systemd، سكريبتات watchdog، سياسات إعادة التشغيل واستخدام tmux/screen.
جدول المحتويات
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
sudo nano /etc/systemd/system/gameserver.service
[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
# 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
| Option | Description |
|---|---|
| no | No restart |
| on-success | Only on successful exit |
| on-failure | On error (exit code != 0) |
| on-abnormal | On signal or timeout |
| on-watchdog | On watchdog timeout |
| always | Always restart |
Recommended Configuration
[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
sudo systemctl reset-failed gameserver
sudo systemctl start gameserver
Watchdog Script
Simple Watchdog
nano ~/watchdog.sh
#!/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
chmod +x ~/watchdog.sh
Advanced Watchdog (Port Check)
nano ~/watchdog_advanced.sh
#!/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
chmod +x ~/watchdog_advanced.sh
Running Watchdog with systemd
sudo nano /etc/systemd/system/gameserver-watchdog.service
[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
sudo systemctl daemon-reload
sudo systemctl enable gameserver-watchdog
sudo systemctl start gameserver-watchdog
Server Management with tmux
tmux Installation
sudo apt install tmux -y
Starting Server with tmux
nano ~/start_tmux.sh
#!/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"
chmod +x ~/start_tmux.sh
tmux Basic Commands
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
sudo apt install screen -y
Starting Server with screen
nano ~/start_screen.sh
#!/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"
chmod +x ~/start_screen.sh
screen Basic Commands
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
nano ~/auto_update_restart.sh
#!/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
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.
مقالات ذات صلة
كيفية تثبيت خادم FiveM: دليل خطوة بخطوة
دليل شامل لتثبيت خادم FiveM على REXE VDS: إعداد txAdmin، إعدادات المنافذ وتحسين الأداء لخوادم GTA V Roleplay. ابدأ خادمك في دقائق.
كيفية تثبيت خادم Minecraft: دليل Paper
دليل شامل لتثبيت خادم Paper/Spigot Minecraft على REXE VDS: إعدادات Java، إدارة الإضافات وتحسين الأداء للحصول على تجربة لعب مستقرة.
كيفية تثبيت خادم CS2: دليل شامل
دليل شامل لتثبيت خادم CS2 المخصص على REXE VDS: SteamCMD، رمز GSLT، إعداد الخادم، أوضاع اللعب وحماية DDoS عبر Path Panel.