Valheim Part 2; Keeping it up to date

I recently posted an article about setting up a Valheim server. After everything was set up, I was faced with a problem that comes from the game being under active Development:

Very frequent updates.

My friends (and I) soon grew weary of constant reminders to update the server so I set up a way to do it automatically. First, the organisational problem. When can I restart the server safely, and when does it disturb the least amount of users?

Since I’m a big believer in democracy I decided to use a doodle. Well, not doodle specifically but the DFN Terminplaner, an implementation of Framadate. Maybe I’ll host it on my own server some day? If I do, dear reader, I’ll be sure to tell you about it in a future blog post. But for now, back to the poll. After polling my friends, the results were pretty clear:

The results of a poll. All polled users chose a time between 09:00 and 16:00.

Direct quote: ‘11:00 would be perfect, because that’s when we usually go to sleep.’

Not quite the typical maintenance time for servers, right? Alas, I don’t really care about that. If they are happy with it, I’m happy with it. So I settled on 12:00 to make sure they are sleeping (should work in 99% of cases).

Now I need a way to automatically run the update command.

The “classic” Linux/Unix way would be to use cron. But why use something boring that was invented for V7 Unix (almost 42 years ago!)? systemd can do something similar, with some caveats. So let’s just do that. We will all have to bow to Poettering and Sievers some day, even if we lose our home directories in the process. The old gods are dead. The new ones are just as terrifying. They could only be stopped by a combined uprising of devuan (remember those guys?) and gentoo users. Or maybe that one guy that approached me during a Linux install party because he wanted to use eduroam on his NixOS machine without using wpa_supplicant.

Anyway.

I decided to use systemd timers.

With this approach we need two systemd units. A service that runs the program and a timer that specifies the time.

update-valheim.service:

[Unit]
Description=Update Valheim server
After=network-online.target

[Service]
WorkingDirectory=/home/steam/Valheim/
ExecStart=/home/steam/update_valheim.sh
Type=oneshot

[Install]
WantedBy=multi-user.target

update-valheim.timer:

[Unit]
Description=Valheim updater timer

[Timer]
OnCalendar=*-*-* 12:00:00

[Install]
WantedBy=timers.target

The line OnCalendar=*-*-* 12:00:00 specifies that we want to execute it every day at 12:00.After that, we just start/enable the timer

systemctl --user start update-valheim.timer
systemctl --user enable update-valheim.timer

The script to actually update the server has the following content:

#!/bin/bash

OUTPUT=$(steamcmd +login anonymous +force_install_dir /home/steam/Valheim +app_update 8966 60 +quit | tail -n 1)

# If app is out of date, restart Service
if ! [[ "$OUTPUT" == "Success! App '896660' already up to date." ]]; then
        echo "Restarting valheim"
        systemctl --user restart valheim.service
fi

If steamCMD detects that the app is already up to date it will output Success! App '896660' already up to date.

We don’t restart the server, if steamCMD didn’t change any of the files. If it did, we restart the service with systemd. And that’s that. A pretty simple solution for a pretty annoying problem. Hopefully it works. If it doesn’t I will of course update you, dear reader!


Articles from blogs I read - Generated by openring