Advertisement

Automatic restarts for a Valheim server

A Valheim server does run better with the occasional restart after long uptimes, but the game offers almost no tools for doing it nicely. The dedicated server has no remote console of any kind.

When should it restart?

Valheim has no remote console, so no script can warn players or tell the server to save. All that remains is a clean process signal, which this game does honour properly. That is why the schedule below fires at exactly the time you chose, with no lead time: there is no countdown to wait for.

What happens, in order

  1. Restart timeStopSend SIGTERM to the process, then let it exit by itself

Valheim’s dedicated server has no remote console, so there is no way to warn players from a script and no save command to send. The restart is a clean process signal and nothing else — which the server does handle correctly, saving the world as it shuts down.

The crontab line

No head start required — nothing counts down before the restart.

0 4 * * *  /opt/restart-valheim.sh

The script it runs

Save it as /opt/restart-valheim.sh and mark it executable. It expects rcon-cli on the path and takes the password from an environment variable instead of storing it in the file.

#!/usr/bin/env bash
set -euo pipefail

# Scheduled restart for Valheim.
# Generated by spawnbench.com/restart-scheduler

sleep 10
# Valheim has no console, so stop it the way its service manager would.
systemctl restart valheim || pkill -TERM -f valheim

Heads up: cron follows the server clock, not your local time. Check with timedatectl first. A container set to UTC and an admin living in Europe differ by two hours in summer and by one in winter, so 04:00 may not be the 04:00 you had in mind.

Advertisement

What is different about Valheim

A script cannot warn players, and there is no save command to send. What Valheim does get right is saving on a clean shutdown, so SIGTERM — the signal a systemctl restart sends — leaves the world safe. Avoid any kill signal that bypasses that, and do not schedule restarts while people play, since the disconnect will be the first warning they get.

Advertisement

Restarts can stop your backups

Valheim's automatic backups are timed from when the server started: the first one is taken after two hours (-backupshort, 7200 seconds) and the rest every twelve (-backuplong, 43200 seconds). Restart the server more often than every two hours and the first backup never comes round. With a daily restart and the defaults, backups land two and fourteen hours after each restart, and nothing in between. If you restart on a schedule, shorten -backuplong as well, or take your own copy of the world before each restart.

Give the save time to finish

A clean stop makes the server write the world before it exits, and a large world takes a while to write. If the server runs under systemd, the service is killed outright when it has not stopped within the stop timeout, 90 seconds by default. That kill skips the save. Set TimeoutStopSec in the unit to a few minutes for an established world, and check that the .db file's modified time changes after a restart; if it does not, the save is not happening.

Warning players without a console

Because there is no way to send a message into the game, the only warning you can give is a predictable one. Pick a fixed time when nobody usually plays, keep to it every day, and say so wherever your group talks. A restart that always happens at 05:00 surprises nobody after the first week.

Rather not run the server yourself?

These tools are free and funded by the two hosting services we run. If you want a server that is already set up, here they are.

Advertisement