Auto-Restarting Your CS 1.6 Server with cron

December 17, 2025 Daemon666 8 min read 17 просмотров

A scheduled restart is different from crash recovery. Crash recovery (covered here) brings a dead server back; a cron restart deliberately bounces a healthy server on a schedule to clear slow memory growth, re-read configs cleanly, and reset any plugin that leaks over days of uptime. Long-running GoldSrc servers do drift — edicts, memory, the occasional stuck plugin — and a nightly restart at a dead hour keeps things fresh. The trick is doing it without yanking players out mid-round.

1. Decide what \"restart\" means for your setup

How you restart depends on how you run the server:

  • systemd unit — the clean case. systemctl restart cs16 does everything. This is the recommended supervisor; see the auto-restart guide.
  • screen/tmux loop — you kill the engine and let the wrapper loop restart it.
  • bare hlds_run — you should not be running it this way for scheduled restarts; wrap it first.

The examples below assume systemd, with a screen alternative noted.

2. The simplest cron restart

For a systemd-managed server, a nightly restart is one crontab line. Edit root's crontab (systemctl needs privilege) or use a sudoers rule for the game user:

crontab -e
# min hour dom mon dow  command
0 6 * * *  systemctl restart cs16

That bounces the server every day at 06:00. Pick an hour when nobody is on — check your own player-count graph rather than guessing. For a screen loop instead, the job kills the engine and the loop restarts it:

0 6 * * *  pkill -f hlds_linux

3. Warn players before you pull the plug

Restarting on top of a full server is rude and loses regulars. If people might be on at your restart hour, announce it first via RCON, then restart a minute later. A tiny wrapper script:

#!/bin/bash
# scheduled-restart.sh
RCON() { /home/steam/tools/rcon "$1"; }   # your rcon helper
RCON 'say [SERVER] Scheduled restart in 60 seconds...'
sleep 30
RCON 'say [SERVER] Restart in 30 seconds - reconnect after.'
sleep 30
systemctl restart cs16
# crontab
0 6 * * *  /home/steam/tools/scheduled-restart.sh

Even better, gate it on emptiness so you never interrupt a live game.

4. Only restart when the server is empty

The considerate pattern is a restart window rather than a fixed instant: check the player count every few minutes during the small hours and restart the first time it is zero. Query the player count (via an RCON status parse, a stats query, or an AMXX-exposed metric) and act on it:

#!/bin/bash
# restart-if-empty.sh -- run every 5 min from 04:00-06:00
PLAYERS=$(/home/steam/tools/rcon 'status' | grep -c '^# ')
STAMP=/tmp/cs-restarted-today
if [ "$PLAYERS" -eq 0 ] && [ ! -f "$STAMP" ]; then
  systemctl restart cs16
  touch "$STAMP"
fi
# crontab -- every 5 min in the quiet window, plus a daily flag reset
*/5 4-6 * * *  /home/steam/tools/restart-if-empty.sh
0 7 * * *      rm -f /tmp/cs-restarted-today

The stamp file ensures you restart at most once per night even though the check runs many times. If the server never empties, it simply is not restarted that day — which is usually the right call for a popular server.

5. Make sure configs actually reload

A restart is only useful if the fresh process reads your current configs. Confirm server.cfg executes on startup (a surprising number of servers have a broken exec chain — see why server.cfg is not executing) and that your first map applies the settings you expect. A nightly restart that reloads a stale or broken config just spreads the problem.

Common errors

  • Cron job does nothingsystemctl needs privilege the crontab user lacks. Put the job in root's crontab or grant a specific sudoers rule.
  • Server restarts mid-match — a fixed-time restart landed on a full server. Switch to the empty-check window, and always warn via RCON.
  • Restarts several times in the window — missing the once-per-night stamp file, so every empty check fires. Add the flag and reset it daily.
  • screen restart leaves no server — you killed the engine but there was no wrapper loop to bring it back. Verify the loop or use systemd.
  • Time zone surprise — cron runs in the server's local time, not your players'. Set the box time zone or offset the schedule to your audience's quiet hours.

Verification

Test the script by hand before trusting cron, and confirm the server comes back:

/home/steam/tools/scheduled-restart.sh
systemctl status cs16        # active (running), fresh uptime
journalctl -u cs16 -n 20     # startup log, server.cfg executed

Watch the console for your hostname and cvar echoes to confirm the config reloaded. Let one real scheduled run happen overnight and check the next morning that uptime reset once, not five times. Pair this with a working crash supervisor and a backup routine for a server that looks after itself.

Участники: Daemon666 ✦
Поделиться: