From b2289a8b46d049b2f5785de601f21b619c6045e4 Mon Sep 17 00:00:00 2001 From: mbround18 <12646562+mbround18@users.noreply.github.com> Date: Wed, 10 Feb 2021 14:34:48 -0700 Subject: [PATCH 1/2] Moved trap to init --- Dockerfile | 2 +- src/scripts/entrypoint.sh | 2 +- src/scripts/init.sh | 15 +++++++++++++-- src/scripts/shutdown.sh | 7 +++++++ 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 src/scripts/shutdown.sh diff --git a/Dockerfile b/Dockerfile index f598a46d..be0bf84f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -65,4 +65,4 @@ RUN usermod -u ${PUID} steam \ && groupmod -g ${PGID} steam \ && chsh -s /bin/bash steam -ENTRYPOINT ["/init.sh"] +ENTRYPOINT ["/bin/bash","/init.sh"] diff --git a/src/scripts/entrypoint.sh b/src/scripts/entrypoint.sh index c860a3f2..45d1c31e 100644 --- a/src/scripts/entrypoint.sh +++ b/src/scripts/entrypoint.sh @@ -36,13 +36,13 @@ log "Starting server..." odin start || exit 1 +trap 'cleanup' INT TERM EXIT cleanup() { log "Halting server! Received interrupt!" odin stop exit } -trap 'cleanup' INT TERM EXIT SIGINT SIGTERM initialize " Valheim Server Started... diff --git a/src/scripts/init.sh b/src/scripts/init.sh index 12542bdc..c8dafa5d 100644 --- a/src/scripts/init.sh +++ b/src/scripts/init.sh @@ -56,7 +56,7 @@ write_env_var() { # shellcheck disable=SC2039 VARIABLE_VALUE=$(printf '%s\n' "${!env_name}" | tr -d '"') echo "Writing $1 to env file..." - if [ $2 = true ]; then + if [[ $2 = true ]]; then echo "${env_name}=\"${VARIABLE_VALUE}\"" >> /home/steam/.env else echo "${env_name}=${VARIABLE_VALUE}" >> /home/steam/.env @@ -71,6 +71,17 @@ write_env_var "PUBLIC" write_env_var "PASSWORD" true write_env_var "AUTO_UPDATE" true -su -s /bin/bash --login steam -c "/bin/bash /home/steam/scripts/entrypoint.sh" +trap 'cleanup' INT TERM EXIT +cleanup() { + echo "Running Cleanup!....." + su -s /bin/bash --login steam -c "/home/steam/scripts/shutdown.sh" + exit 0 +} + +su -s /bin/bash --login steam -c "/home/steam/scripts/entrypoint.sh" + +while :; do + sleep 1s +done diff --git a/src/scripts/shutdown.sh b/src/scripts/shutdown.sh new file mode 100644 index 00000000..a1063daf --- /dev/null +++ b/src/scripts/shutdown.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +. /home/steam/scripts/load_env.sh +echo " +Shutting down the server......... +" +cd /home/steam/valheim || exit 1 +odin stop || exit 1 From 18c9cdf2cce0c583baf25256984d41d2b0a1fec5 Mon Sep 17 00:00:00 2001 From: mbround18 <12646562+mbround18@users.noreply.github.com> Date: Wed, 10 Feb 2021 14:47:32 -0700 Subject: [PATCH 2/2] Added gosu --- Dockerfile | 3 ++- src/scripts/init.sh | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index be0bf84f..78e781d7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,7 +31,8 @@ RUN apt-get update \ && apt-get install -y \ htop net-tools nano \ netcat curl wget \ - cron sudo + cron sudo gosu \ + && gosu nobody true # Set up timezone information ENV TZ=America/Los_Angeles diff --git a/src/scripts/init.sh b/src/scripts/init.sh index c8dafa5d..1582396f 100644 --- a/src/scripts/init.sh +++ b/src/scripts/init.sh @@ -76,11 +76,11 @@ trap 'cleanup' INT TERM EXIT cleanup() { echo "Running Cleanup!....." - su -s /bin/bash --login steam -c "/home/steam/scripts/shutdown.sh" + gosu steam:steam /home/steam/scripts/shutdown.sh exit 0 } -su -s /bin/bash --login steam -c "/home/steam/scripts/entrypoint.sh" +gosu steam:steam /home/steam/scripts/entrypoint.sh while :; do sleep 1s