Auto update script for Valheim server on Ubuntu
Solution 1:
I am hosting a valheim server for a friend of mine on my dedicated server (Debian 10) and ran into the same issue you did. I created a bash script to update the server automatically.
Place this script within any folder you want to have it inside. I preferred to put it in the valheim directory. Then change the values inside the script to correspond to your environment.
Then execute "chmod +x [script path]" to make it executable.
Then create an entry within your /etc/crontab file looking like this (this will update the server at 4AM every day (change the 4 to anything else if you want to). Don't forget to use the correct script path.
0 4 * * * root /home/steam/valheim/update_server.sh
Script (update_valheim.sh)
#!/bin/bash
# BEGIN - EDIT THOSE LINES TO CORRESPOND TO YOUR ENVIRONMENT
# Where should the update script log to?
LOGNAME="/var/log/valheim-update.log"
# Path of your steamcmd executable
STEAMCMD="/home/steam/steamcmd"
# Start timestamp - don't change this.
STA_TIMESTAMP=`date "+%Y-%m-%d_%H:%M:%S"`
# Installation directory of your dedicated server
VALHEIM_INSTALL_DIRECTORY="/home/steam/valheim"
# Command to run after updating the server
VALHEIM_SERVICE_RESTART="service valheim restart"
# The user to execute the steamcmd with (to not run it as root preventing security issues)
VALHEIM_USER="steam"
# END - THE REST SHOULDN'T BE EDITED
printf '\n[*] %s - Updating valheim dedicated server via cronjob...\n\n' $STA_TIMESTAMP >> "$LOGNAME"
# We prevent permission issues when this script is placed inside the valheim folder as root user. ;)
chown -R $VALHEIM_USER $VALHEIM_INSTALL_DIRECTORY
su - $VALHEIM_USER -c "$STEAMCMD +login anonymous +force_install_dir $VALHEIM_INSTALL_DIRECTORY +app_update 896660 validate +exit" >> "$LOGNAME" 2>&1
printf '\n[*] Restarting service...\n\n' >> "$LOGNAME"
END_TIMESTAMP=`date "+%Y-%m-%d_%H:%M:%S"`
$VALHEIM_SERVICE_RESTART >> "$LOGNAME" 2>&1
if [ $? -eq 0 ]; then
printf '\n[*] Service successfully restarted\n\n%s - Update succeeded\n' $END_TIMESTAMP >> "$LOGNAME"
else
printf '\n[!] CRITICAL: Error while restarting the service (ERRORLEVEL does not equal ZERO)\n\n%s - Update failed\n' $END_TIMESTAMP >> "$LOGNAME"
fi
Kind regards from Germany and have fun
Thomas
--- EDIT ---
If you want to enable other people to execute the script if you aren't home you could use a PHP script on your webserver (if you got one) and add a sudo entry for the www-data user to execute the script as root aswell. Just don't forget to protect the corresponding link with a .htaccess file.
/etc/sudoers
www-data ALL= NOPASSWD: /home/steam/valheim/update_server.sh
PHP
<?php
set_time_limit(500);
shell_exec("/home/steam/valheim/update_server.sh");