cat /proc/claus

because blogs never go out of style!

Setting up a restart service for my Valheim server

2025 February 08

For the past three or four months, I have been hosting a Valheim server to play with my friends. The server runs on the VPS that is also used to run this blog. However, for some reason, every now and then the server goes down. From the logs, I can't really figure out why the server is going down, but my guess is that it has something to do with the save game backups.

Since I can't figure out how to stop the server from going down, the second best thing is to automate the process of starting it up again. In theory, it is simple:

  • Make a script that checks if the server is down, and if it is, start it again;
  • Put that script to run every now and then using cron

In practice, I realized I forgot a lot of stuff about bash scripting and cron, so I decided to write down what I had to re-learn to make this work.

As an aside, search engines REALLY suck these days for looking up anything at all. So much AI slop on the results... :-(

Part 1: Script to re-launch the server

The basic to launch the server is pretty simple:

if [[ `ps -A|grep valheim` ]]; then
   :
else
   echo `date +%Y-%m-%d_%H:%M:%S` Valheim is NOT running. Restarting. >> /home/steam/vrestart.log
   cd /home/steam/Valheim
   /home/steam/Valheim/valheim.sh
fi

It searches the output of the ps -A command (which lists all processes), and if it finds a process with valheim in its name, it decides that the server is running, and does nothing. Otherwise, it registers that the server is not running in the log, and launches the server again.

One thing to note is the : on the true side of the if statement. That is what a no-op in bash looks like, as I learned in this stack overflow answer.

Another thing to note is that the script does not use nohup, which is a command that you use when launching services by hand over ssh. It turns out that it is not necessary when launching services through cron, as stated in this stack overflow answer.

Problem 0: this script would get confused if we have some other process with valheim in its name. A more robust way to do this is to somehow get the pid of the server process and use that instead. But that is another script, to be written another day.

Part 2: Putting that script on cron

cron is a service that allows you to run programs on a fixed time interval, such as every day, or every other hour, or every Tuesday, 17:32. In this case, we want to run our server restart script every 10 minutes, so we create a file with the following content:

*/5 * * * * /home/steam/vrestart.sh >steam_cron.log 2>&

Then we feed this file to our user's cron table using the following command:

crontab -u steam /home/steam/configfiles/crontab.steam

Problem 1: When I did this, I got an error on the script file, saying that it did not recognize the [[ on the if condition. This is because that construct only works in bash, and cron seems to run the scripts using sh by default. We need to add the following line to the top of the re-start script to indicate that it needs to be ran using bash:

#!/bin/bash

Problem 2: The re-start script is now running, but the timestamps of the log are wrong! This is because, again, the environment used by cron is not the same as the environment of the user. To solve this, we need to indicate to cron which timezone is to be used with the script (as indicated on the second answer of this askubuntu thread)

*/5 * * * * TZ="Asia/Tokyo" /home/steam/vrestart.sh >steam_cron.log 2>&

Done!

Now my friends should be able to get their valheim fix, even when I'm sleeping!

On to the deep north!

Tagged: #valheim, #linux, #cron,