From 2295825b35a34e22c9a8a5603f86595f970b20de Mon Sep 17 00:00:00 2001 From: Peter Siegmund Date: Sat, 17 Jan 2026 15:37:34 +0100 Subject: [PATCH] initial Signed-off-by: Peter Siegmund --- README.md | 67 +++++++++++++++++++++++++++++++++++++++++++ hytale-update.service | 12 ++++++++ hytale-update.sh | 66 ++++++++++++++++++++++++++++++++++++++++++ hytale-update.timer | 9 ++++++ hytale.service | 27 +++++++++++++++++ 5 files changed, 181 insertions(+) create mode 100644 README.md create mode 100644 hytale-update.service create mode 100644 hytale-update.sh create mode 100644 hytale-update.timer create mode 100644 hytale.service diff --git a/README.md b/README.md new file mode 100644 index 0000000..2fbb64d --- /dev/null +++ b/README.md @@ -0,0 +1,67 @@ +# Hytale Server Systemd Setup + +## Files + +| File | Description | Target on Server | +|------|-------------|------------------| +| `hytale.service` | Main service for the server | `/etc/systemd/system/` | +| `hytale-update.sh` | Update script | `/opt/hytale/` | +| `hytale-update.service` | Service for updates | `/etc/systemd/system/` | +| `hytale-update.timer` | Timer for daily updates at 4 AM | `/etc/systemd/system/` | + +## Installation on the Server + +```bash +# 1. Copy files +sudo cp hytale.service /etc/systemd/system/ +sudo cp hytale-update.service /etc/systemd/system/ +sudo cp hytale-update.timer /etc/systemd/system/ +cp hytale-update.sh /opt/hytale/ +chmod +x /opt/hytale/hytale-update.sh + +# 2. Create log file +sudo touch /var/log/hytale-update.log +sudo chown mars3142:mars3142 /var/log/hytale-update.log + +# 3. Reload systemd +sudo systemctl daemon-reload + +# 4. Enable and start server service +sudo systemctl enable hytale.service +sudo systemctl start hytale.service + +# 5. Enable update timer +sudo systemctl enable hytale-update.timer +sudo systemctl start hytale-update.timer +``` + +## Useful Commands + +```bash +# Server status +sudo systemctl status hytale.service + +# Show server logs +sudo journalctl -u hytale.service -f + +# Manually stop/start server +sudo systemctl stop hytale.service +sudo systemctl start hytale.service +sudo systemctl restart hytale.service + +# Run update manually +sudo /opt/hytale/hytale-update.sh + +# Check timer status +sudo systemctl list-timers | grep hytale + +# Show update logs +cat /var/log/hytale-update.log +``` + +## Notes + +- The server starts automatically after a reboot +- Updates are checked daily at 4:00 AM +- Already applied updates are stored in `/opt/hytale/.applied_updates` +- When an update is found, the server is automatically stopped, updated, and restarted diff --git a/hytale-update.service b/hytale-update.service new file mode 100644 index 0000000..74b2f4e --- /dev/null +++ b/hytale-update.service @@ -0,0 +1,12 @@ +[Unit] +Description=Hytale Server Update Service +After=network.target + +[Service] +Type=oneshot +ExecStart=/opt/hytale/hytale-update.sh +User=root +Group=root + +[Install] +WantedBy=multi-user.target diff --git a/hytale-update.sh b/hytale-update.sh new file mode 100644 index 0000000..1b1f940 --- /dev/null +++ b/hytale-update.sh @@ -0,0 +1,66 @@ +#!/bin/bash +# +# Hytale Server Update Script +# Runs the updater, extracts new files, and restarts the server +# + +set -e + +HYTALE_DIR="/opt/hytale" +LOG_FILE="/var/log/hytale-update.log" + +log() { + echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE" +} + +cd "$HYTALE_DIR" + +log "Starting update check..." + +# Run the updater to check for and download updates +# The updater downloads files like "2026-01-17-abcdef.zip" +./updater 2>&1 | tee -a "$LOG_FILE" + +# Find the newest zip file (pattern: YYYY-MM-DD-*.zip) +NEWEST_ZIP=$(ls -t [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-*.zip 2>/dev/null | head -n1) + +if [ -z "$NEWEST_ZIP" ]; then + log "No update zip found. Nothing to do." + exit 0 +fi + +# Check if this update was already applied +APPLIED_MARKER=".applied_updates" +if grep -Fxq "$NEWEST_ZIP" "$APPLIED_MARKER" 2>/dev/null; then + log "Update $NEWEST_ZIP was already applied. Skipping." + exit 0 +fi + +log "New update found: $NEWEST_ZIP" + +# Stop the server +log "Stopping Hytale server..." +systemctl stop hytale.service || true + +# Wait for server to fully stop +sleep 5 + +# Extract the update directly to /opt/hytale +log "Extracting $NEWEST_ZIP to $HYTALE_DIR..." +unzip -o "$NEWEST_ZIP" -d "$HYTALE_DIR" + +# Fix file ownership (since this script runs as root) +chown -R mars3142:mars3142 "$HYTALE_DIR" + +# Mark this update as applied +echo "$NEWEST_ZIP" >> "$APPLIED_MARKER" + +# Start the server again +log "Starting Hytale server..." +systemctl start hytale.service + +# Cleanup: keep only the 3 newest zip files +log "Cleaning up old zip files..." +ls -t [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-*.zip 2>/dev/null | tail -n +4 | xargs -r rm -f + +log "Update complete!" diff --git a/hytale-update.timer b/hytale-update.timer new file mode 100644 index 0000000..739bdf7 --- /dev/null +++ b/hytale-update.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Daily Hytale Server Update Check at 4:00 AM + +[Timer] +OnCalendar=*-*-* 04:00:00 +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/hytale.service b/hytale.service new file mode 100644 index 0000000..2b3cf08 --- /dev/null +++ b/hytale.service @@ -0,0 +1,27 @@ +[Unit] +Description=Hytale Game Server +After=network.target + +[Service] +Type=simple +User=mars3142 +Group=mars3142 +WorkingDirectory=/opt/hytale/Server + +# SDKMAN Java path for user mars3142 +Environment="JAVA_HOME=/home/mars3142/.sdkman/candidates/java/current" +Environment="PATH=/home/mars3142/.sdkman/candidates/java/current/bin:/usr/local/bin:/usr/bin:/bin" + +ExecStart=/home/mars3142/.sdkman/candidates/java/current/bin/java -jar HytaleServer.jar --assets ../Assets.zip + +Restart=on-failure +RestartSec=10 + +# Security hardening +NoNewPrivileges=true +ProtectSystem=strict +ReadWritePaths=/opt/hytale +ProtectHome=read-only + +[Install] +WantedBy=multi-user.target