Having recently setup a dedicated Valheim game server, I wanted to share the systemd files I used to orchestrate and automate running and updating the server.
These notes assume that you've setup your Valheim game server using LinuxGSM according to https://linuxgsm.com/lgsm/vhserver/. I'm using Ubuntu 20.04 to host the game, but any LinuxGSM compatible distro with systemd should work.
You should also be able to modify these files to run other LinuxGSM game servers.
LinuxGSM has a page the references this: https://docs.linuxgsm.com/configuration/running-on-boot
I used that template to make /etc/systemd/system/valheim.service
:
[Unit]
Description=LinuxGSM Valheim Server
After=network-online.target
Wants=network-online.target
[Service]
Type=forking
User=vhserver
WorkingDirectory=/home/vhserver
RemainAfterExit=yes
ExecStart=/home/vhserver/vhserver start
ExecStop=/home/vhserver/vhserver stop
Restart=on-failure
RestartSec=30s
[Install]
WantedBy=multi-user.target
LinuxGSM recommends scheduling the following functions:
- Running
vhserver monitor
every 5 minutes, to check the server status and restart if necessary. - Running
vhserver update
every 30 minutes, to update the game itself via Steam (I've modified to run hourly) - Running
vhserver update-lgsm
every day, to update the LinuxGSM wrapper files (Modified to run weekly)
However, they recommend scheduling these with cron:
*/5 * * * * /home/vhserver/vhserver monitor > /dev/null 2>&1
*/30 * * * * /home/vhserver/vhserver update > /dev/null 2>&1
0 0 * * 0 /home/vhserver/vhserver update-lgsm > /dev/null 2>&1
These functions can be translated to systemd. The following are "oneshot" services (a service that runs and exits) and corresponding timers, which calls the services at a specified interval.
Oneshot service: /etc/systemd/system/valheim-monitor.service
[Unit]
Description=Check status of LinuxGSM Valheim Server
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
User=vhserver
WorkingDirectory=/home/vhserver
ExecStart=/home/vhserver/vhserver monitor
[Install]
WantedBy=multi-user.target
Timer, scheduled every 5 minutes: /etc/systemd/system/valheim-monitor.timer
:
[Unit]
Description=Check LinuxGSM Valheim Server Status
Requires=valheim-monitor.service
[Timer]
Unit=valheim-monitor.service
OnCalendar=*:0/5
[Install]
WantedBy=timers.target
Oneshot service: /etc/systemd/system/valheim-update.service
[Unit]
Description=Update LinuxGSM Valheim Server Files
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
User=vhserver
WorkingDirectory=/home/vhserver
ExecStart=/home/vhserver/vhserver update
[Install]
WantedBy=multi-user.target
Timer, scheduled every hour: /etc/systemd/system/valheim-update.timer
:
[Unit]
Description=Update LinuxGSM Valheim Server Files
Requires=valheim-update.service
[Timer]
Unit=valheim-update.service
OnCalendar=hourly
[Install]
WantedBy=timers.target
Oneshot service: /etc/systemd/system/valheim-lgsm-update.service
:
[Unit]
Description=Update LinuxGSM wrapper files for Valheim server
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
User=vhserver
WorkingDirectory=/home/vhserver
ExecStart=/home/vhserver/vhserver update-lgsm
[Install]
WantedBy=multi-user.target
Timer, scheduled every week: /etc/systemd/system/valheim-lgsm-update.service
:
[Unit]
Description=Update Valheim LinuxGSM wrapper files
Requires=valheim-lgsm-update.service
[Timer]
Unit=valheim-lgsm-update.service
OnCalendar=weekly
[Install]
WantedBy=timers.target
(If you're new to this, remember to prefix these with sudo
if you're not already logged in as root.)
First, tell systemd to reload the service files:
systemctl daemon-reload
Don't forget to enable and start the game itself (after configuring it of course; not covered in this Gist).
systemctl enable valheim
systemctl start valheim
Then, enable and start the timers (no need to do anything with the helper services themselves):
systemctl enable valheim-monitor.timer
systemctl enable valheim-update.timer
systemctl enable valheim-lgsm-update.timer
systemctl start valheim-monitor.timer
systemctl start valheim-update.timer
systemctl start valheim-lgsm-update.timer
That's it!