I have a server for the game Valheim running as a service headless in ubuntu. It will run automatically when the appropriate user logs in.
However, I have run into a slight problem. Any time the client game updates the server needs to be up to date, less they are denied a connection. While I have seen sudo crontab updates for the system its self, I don't know how to set up a service to update the game. currently, I have 3 minimum commands to update the client.
- sudo systemctl stop valheim
- /home/username/.steam/steamcmd/steamcmd.sh +login anonymous +force_install_dir /home/username/Valheim +app_update 896660 validate +exit
- sudo systemctl stop valheim
Is there a way to make an auto-run file to do this with sudo password? Bonus points if this option could be launched remotely if I'm not at home to run the file.
2 Answers
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"); Wouldn't this work with root crontab?
sudo crontab -e the cron:
@hourly echo "yourpassword" | sudo myfile.sh Your password will be exposed to anyone who has access to that cron though, not a huge deal but keep it in mind.