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
This commit is contained in:
Kris Lamoureux 2024-09-19 23:22:14 -04:00
parent e9399f4344
commit 8fa2d12bc9
Signed by: kris
GPG Key ID: 3EDA9C3441EDA925
2 changed files with 22 additions and 5 deletions

View File

@ -22,7 +22,6 @@ RUN apt-get update && \
RUN curl -s -o "/tmp/libtorrent-${LIBTORRENT_VERSION}.tar.gz" -L "$LIBTORRENT_URL" && \ 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)" && \ FILE_HASH="$(sha256sum /tmp/libtorrent-${LIBTORRENT_VERSION}.tar.gz | cut -d' ' -f1)" && \
echo hey; \
du -sh "/tmp/libtorrent-${LIBTORRENT_VERSION}.tar.gz"; \ du -sh "/tmp/libtorrent-${LIBTORRENT_VERSION}.tar.gz"; \
if [ ! "$LIBTORRENT_HASH" = "$FILE_HASH" ]; then \ if [ ! "$LIBTORRENT_HASH" = "$FILE_HASH" ]; then \
echo "SHA256 verification failed!" && \ echo "SHA256 verification failed!" && \
@ -69,7 +68,9 @@ COPY --from=build /tmp/rtorrent.rc.template /tmp/rtorrent.rc.template
RUN apt-get update && \ RUN apt-get update && \
apt-get install -y \ apt-get install -y \
libcurl4 \ libcurl4 \
libncursesw6 && \ libncursesw6 \
procps \
screen && \
apt-get clean && \ apt-get clean && \
rm -rf /var/lib/apt/lists/* rm -rf /var/lib/apt/lists/*

View File

@ -1,6 +1,9 @@
#!/bin/bash #!/bin/bash
set -eu set -eu
# Polling interval
SLEEP_INTERVAL=${SLEEP_INTERVAL:-2}
# GID/UID # GID/UID
USER_ID=${GUID:-1000} USER_ID=${GUID:-1000}
GROUP_ID=${PGID:-1000} GROUP_ID=${PGID:-1000}
@ -12,7 +15,7 @@ id -u rtorrent &>/dev/null || \
# Update group/user IDs # Update group/user IDs
groupmod -o -g "$GROUP_ID" rtorrent 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 # Copy default config
if [ ! -e /home/rtorrent/.rtorrent.rc ]; then 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 chown rtorrent:rtorrent /home/rtorrent/.rtorrent.rc
fi fi
# Execute as the rtorrent user # Start rtorrent in a detached screen session
su - rtorrent -c "rtorrent" 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