diff --git a/.env.example b/.env.example index 448755c47..f51a179e8 100644 --- a/.env.example +++ b/.env.example @@ -22,6 +22,9 @@ BACKUP_CRON_EXPRESSION=0 0 * * * AUTO_UPDATE_ENABLED=false AUTO_UPDATE_CRON_EXPRESSION=0 * * * * AUTO_UPDATE_WARN_MINUTES=30 +AUTO_REBOOT_ENABLED=false +AUTO_REBOOT_WARN_MINUTES=5 +AUTO_REBOOT_CRON_EXPRESSION=0 0 * * * DIFFICULTY=None DAYTIME_SPEEDRATE=1.000000 diff --git a/Dockerfile b/Dockerfile index 40d18c98d..963f15a8f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -47,10 +47,13 @@ ENV PORT= \ BACKUP_CRON_EXPRESSION="0 0 * * *" \ AUTO_UPDATE_ENABLED=false \ AUTO_UPDATE_CRON_EXPRESSION="0 * * * *" \ - AUTO_UPDATE_WARN_MINUTES=30 + AUTO_UPDATE_WARN_MINUTES=30 \ + AUTO_REBOOT_ENABLED=false \ + AUTO_REBOOT_WARN_MINUTES=5 \ + AUTO_REBOOT_CRON_EXPRESSION="0 0 * * *" COPY ./scripts/* /home/steam/server/ -RUN chmod +x /home/steam/server/init.sh /home/steam/server/start.sh /home/steam/server/backup.sh /home/steam/server/update.sh && \ +RUN chmod +x /home/steam/server/*.sh && \ mv /home/steam/server/backup.sh /usr/local/bin/backup && \ mv /home/steam/server/update.sh /usr/local/bin/update diff --git a/README.md b/README.md index e308a6e4a..85e29814d 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,9 @@ It is highly recommended you set the following environment values before startin | AUTO_UPDATE_CRON_EXPRESSION | Setting affects frequency of automatic updates. | 0 \* \* \* \* | Needs a Cron-Expression - See [Configuring Automatic Backups with Cron](#configuring-automatic-backups-with-cron) | | AUTO_UPDATE_ENABLED | Enables automatic updates | false | true/false | | AUTO_UPDATE_WARN_MINUTES | How long to wait to update the server, after the player were informed. | 30 | !0 | +| AUTO_REBOOT_CRON_EXPRESSION | Setting affects frequency of automatic updates. | 0 0 \* \* \* | Needs a Cron-Expression - See [Configuring Automatic Backups with Cron](#configuring-automatic-reboots-with-cron) | +| AUTO_REBOOT_ENABLED | Enables automatic reboots | false | true/false | +| AUTO_REBOOT_WARN_MINUTES | How long to wait to reboot the server, after the player were informed. | 5 | !0 | *highly recommended to set @@ -305,6 +308,30 @@ AUTO_UPDATE_CRON_EXPRESSION is a cron expression, in a Cron-Expression you defin Set AUTO_UPDATE_CRON_EXPRESSION to change the default schedule. +## Configuring Automatic Reboots with Cron + +To be able to use automatic reboots with this server RCON_ENABLED enabled. + +> [!IMPORTANT] +> +> If docker restart is not set to policy `always` or `unless-stopped` then the server will shutdown and will need to be +> manually restarted. +> +> The example docker run command and docker compose file in [How to Use](#how-to-use) already use the needed policy + +Set AUTO_REBOOT_ENABLED enable or disable automatic backups (Default is disabled) + +AUTO_REBOOT_CRON_EXPRESSION is a cron expression, in a Cron-Expression you define an interval for when to run jobs. + +> [!TIP] +> This image uses Supercronic for crons +> see [supercronic](https://github.com/aptible/supercronic#crontab-format) +> or +> [Crontab Generator](https://crontab-generator.org). + +Set AUTO_REBOOT_CRON_EXPRESSION to change the set the schedule, default is everynight at midnight according to the +timezone set with TZ + ## Editing Server Settings ### With Environment Variables diff --git a/scripts/auto_reboot.sh b/scripts/auto_reboot.sh new file mode 100644 index 000000000..9fa4e0fde --- /dev/null +++ b/scripts/auto_reboot.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +if [ "${RCON_ENABLED,,}" = true ]; then + if [ -z "${AUTO_REBOOT_WARN_MINUTES}" ]; then + echo "Unable to auto reboot, AUTO_REBOOT_WARN_MINUTES is empty." + elif [[ "${AUTO_REBOOT_WARN_MINUTES}" =~ ^[0-9]+$ ]]; then + for ((i = "${AUTO_REBOOT_WARN_MINUTES}" ; i > 0 ; i--)); do + rcon-cli -c /home/steam/server/rcon.yaml "broadcast The_Server_will_reboot_in_${i}_Minutes" + sleep "1m" + done + + rcon-cli -c /home/steam/server/rcon.yaml save + rcon-cli -c /home/steam/server/rcon.yaml "shutdown 1" + else + echo "Unable to auto reboot, AUTO_REBOOT_WARN_MINUTES is not an integer: ${AUTO_REBOOT_WARN_MINUTES}" + fi +else + echo "Unable to reboot. RCON is required." +fi diff --git a/scripts/start.sh b/scripts/start.sh index 6ad106e90..082d92b1d 100644 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -312,7 +312,13 @@ if [ "${AUTO_UPDATE_ENABLED,,}" = true ] && [ "${UPDATE_ON_BOOT}" = true ]; then echo "$AUTO_UPDATE_CRON_EXPRESSION bash /usr/local/bin/update" >> "/home/steam/server/crontab" fi -if { [ "${AUTO_UPDATE_ENABLED,,}" = true ] && [ "${UPDATE_ON_BOOT,,}" = true ]; } || [ "${BACKUP_ENABLED,,}" = true ]; then +if [ "${AUTO_REBOOT_ENABLED,,}" = true ] && [ "${RCON_ENABLED,,}" = true ]; then + echo "AUTO_REBOOT_ENABLED=${AUTO_REBOOT_ENABLED,,}" + echo "$AUTO_REBOOT_CRON_EXPRESSION bash /home/steam/server/auto_reboot.sh" >> "/home/steam/server/crontab" +fi + +if { [ "${AUTO_UPDATE_ENABLED,,}" = true ] && [ "${UPDATE_ON_BOOT,,}" = true ]; } || [ "${BACKUP_ENABLED,,}" = true ] || \ + [ "${AUTO_REBOOT_ENABLED,,}" = true ]; then supercronic "/home/steam/server/crontab" & fi