Skip to content

Instantly share code, notes, and snippets.

@cnrat
Last active May 16, 2024 19:00
Show Gist options
  • Save cnrat/3605f9892ec535297030fc173d180651 to your computer and use it in GitHub Desktop.
Save cnrat/3605f9892ec535297030fc173d180651 to your computer and use it in GitHub Desktop.
Use systemd to control your Valheim dedicated server.

Use systemd to control your Valheim dedicated server

Based on CentOS 7

Make a config folder to save your world

By default, Valheim dedicated server use ~/.config/unity3d/IronGate/Valheim to save your world according to the Manual (Valheim Dedicated Server Manual.pdf).

As we try to make it a better way to manage the world data, we should specify where to save.

mkdir /home/valheim_world_data

Build a start script

It is recommended using a copy of start_server.sh to avoid getting overwritten when update the server from Steam.

touch /etc/valheim/start.sh
vim /etc/valheim/start.sh
#!/bin/bash

export templdpath=$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=./linux64:$LD_LIBRARY_PATH
export SteamAppId=892970
./valheim_server.x86_64 -name "ExamplesServer" -port 2456 -world "ExampleWorld" -password "secret" -savedir /home/valheim_world_data -public 0 2>&1 | grep --line-buffered -v 'steamnetworkingsockets_lowlevel' >>/var/log/valheim.log & jobs -p >/var/run/valheim.pid
export LD_LIBRARY_PATH=$templdpath

You might want to change ExamplesServer ExampleWorld secret as you prefered.

I know someone use html tag to make a colorful server name like <font color="red">MyServer</font>. In a short way, it's bad, try not to do it.

It's a bit tricky to get the pID of main process while avoiding the log issue.

You might have noticed a lot of error in your log file like src/steamnetworkingsockets/clientlib/steamnetworkingsockets_lowlevel.cpp (1425) : Assertion Failed: SDR service thread gave up on lock after waiting 50ms. This directly adds to delay of processing of network packets! since the latest update.

So I put a grep to filter the log before it write to the file.

That leads to use & jobs -p to get the real pID of main process which command returns the leader process ID only.

And we also need to use --line-buffered to foece grep use line-buffer instead of block-buffer to avoid log line broken.

Build a systemd service

touch /usr/lib/systemd/system/valheim.service 
vim /usr/lib/systemd/system/valheim.service
[Unit]
Description=Valheim Linux Server
After=network.target

[Service]
Type=simple
KillSignal=SIGINT
WorkingDirectory=/etc/valheim
ExecStart=/etc/valheim/start.sh
PIDFile=/var/run/valheim.pid
User=root
LimitNOFILE=51200
LimitCORE=infinity

[Install]
WantedBy=multi-user.target

Change the WorkingDirectory= and ExecStart= if your Valheim dedicated server is not installed in /etc/valheim.

There is a bug not fixed yet that even you are using -savedir in the startup arguments to specify where to save the world, the server still try to access ~/.config/unity3d/IronGate/Valheim/prefs. That's why you should use User= to specify which user root will be used to start the service.

With KillSignal=SIGINT we can shutdown the game server gracefully without losing any data.

Reload the systemd

Every time you modify the service file, use the following command to make it load correctly.

systemctl daemon-reload

Start an stop the Valheim dedicated server

Start

systemctl start valheim.service

Stop

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