Skip to content

Instantly share code, notes, and snippets.

@joelheaps
Last active April 3, 2021 17:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelheaps/4b69f0c2c8bdad97c263952f8011f538 to your computer and use it in GitHub Desktop.
Save joelheaps/4b69f0c2c8bdad97c263952f8011f538 to your computer and use it in GitHub Desktop.
Running LinuxGSM Valheim game server with systemd services and timers

Running a LinuxGSM Valheim Game Server with Systemd Services and Timers

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.

Running Valheim itself as a systemd service

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

Scheduling LinuxGSM functions

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.

1. vhserver monitor

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

2. vhserver update

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

3. vhserver update-lgsm

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

Enable and start everything

(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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment