From 8fa2d12bc96c90c94692ebb0c6fd12bb3029512b Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Thu, 19 Sep 2024 23:22:14 -0400 Subject: [PATCH] Run in screen and handle graceful shutdown - Install screen/procps and run in detached session - Monitor PID and trap signals for clean exit - Suppress stderr/stdout when running usermod - Clean up Dockerfile --- Dockerfile | 5 +++-- entrypoint.sh | 22 +++++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index e92eec3..6899059 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,7 +22,6 @@ RUN apt-get update && \ RUN curl -s -o "/tmp/libtorrent-${LIBTORRENT_VERSION}.tar.gz" -L "$LIBTORRENT_URL" && \ FILE_HASH="$(sha256sum /tmp/libtorrent-${LIBTORRENT_VERSION}.tar.gz | cut -d' ' -f1)" && \ - echo hey; \ du -sh "/tmp/libtorrent-${LIBTORRENT_VERSION}.tar.gz"; \ if [ ! "$LIBTORRENT_HASH" = "$FILE_HASH" ]; then \ echo "SHA256 verification failed!" && \ @@ -69,7 +68,9 @@ COPY --from=build /tmp/rtorrent.rc.template /tmp/rtorrent.rc.template RUN apt-get update && \ apt-get install -y \ libcurl4 \ - libncursesw6 && \ + libncursesw6 \ + procps \ + screen && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* diff --git a/entrypoint.sh b/entrypoint.sh index 9c77626..70ba848 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,6 +1,9 @@ #!/bin/bash set -eu +# Polling interval +SLEEP_INTERVAL=${SLEEP_INTERVAL:-2} + # GID/UID USER_ID=${GUID:-1000} GROUP_ID=${PGID:-1000} @@ -12,7 +15,7 @@ id -u rtorrent &>/dev/null || \ # Update group/user IDs groupmod -o -g "$GROUP_ID" rtorrent -usermod -o -u "$USER_ID" rtorrent 2>/dev/null +usermod -o -u "$USER_ID" rtorrent &>/dev/null # Copy default config if [ ! -e /home/rtorrent/.rtorrent.rc ]; then @@ -20,5 +23,18 @@ if [ ! -e /home/rtorrent/.rtorrent.rc ]; then chown rtorrent:rtorrent /home/rtorrent/.rtorrent.rc fi -# Execute as the rtorrent user -su - rtorrent -c "rtorrent" +# Start rtorrent in a detached screen session +su - rtorrent -c "screen -dmS rtorrent rtorrent" & + +# Wait for the su process to finish +wait $! + +# Find rtorrent PID and monitor it +RTORRENT_PID="$(pgrep -f "^rtorrent$")" + +# Trap for graceful shutdown +trap 'su - rtorrent -c "kill -15 $RTORRENT_PID"' SIGTERM SIGINT + +# Wait for PID +while kill -0 "$RTORRENT_PID" 2>/dev/null; do sleep "$SLEEP_INTERVAL"; done +exit 0