Last active
November 8, 2024 11:44
-
-
Save warmonkey/68d61229f1564dfcfe6d784790ad772e to your computer and use it in GitHub Desktop.
Factorio server auto update script and systemd service file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# if the script hangs on wget download, replace `wget` with `wget -4` | |
: ' | |
/etc/systemd/system/factorio.service | |
[Unit] | |
Descritpion=Factorio Headless Server | |
After=network.target | |
After=systemd-user-sessions.service | |
After=network-online.target | |
[Service] | |
Type=simple | |
User=root | |
ExecStart=/opt/factorio/bin/x64/factorio --start-server-load-latest | |
[Install] | |
WantedBy=multi-user.target | |
' | |
# check permission for writing to /opt | |
if [ "$EUID" -ne 0 ] | |
then echo "Please run as root" | |
exit | |
fi | |
# parse download page | |
stable_version=$(wget -qO- https://www.factorio.com/download | \ | |
grep -oP '(?<=/get-download/)[0-9]+\.[0-9]+\.[0-9]+' | \ | |
sort -Vu | tail -n 1) | |
# validate stable_version | |
if [ -z "$stable_version" ]; then | |
echo "Failed to get stable_version from factorio website" | |
exit 1 | |
fi | |
# get current version | |
current_version=$( /opt/factorio/bin/x64/factorio --version | grep -oP 'Version: \K[0-9]+\.[0-9]+\.[0-9]+' ) | |
# validate current_version | |
if [ -z "$current_version" ]; then | |
echo "Failed to get current_version from factorio local executable" | |
exit 1 | |
fi | |
# compare version | |
echo -e "Online: $stable_version" | |
echo -e "Installed: $current_version" | |
if [ "$stable_version" == "$current_version" ]; then | |
exit 0 | |
fi | |
# apply update | |
wget -O factorio_headless.tar.xz https://www.factorio.com/get-download/stable/headless/linux64 | |
echo "decompressing" | |
tar -xJf factorio_headless.tar.xz | |
echo "stopping factorio" | |
systemctl stop factorio | |
echo "installing files" | |
cp -r factorio /opt/ | |
rm -fr factorio | |
echo "starting factorio" | |
systemctl start factorio |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment