Skip to content

Commit c248913

Browse files
cowwocjaydrogers
andauthored
Fixes #6: Shut down quicker in response to "docker stop" (#7)
* Fixes #6: Shut down quicker in response to "docker stop" * Add support for SIGTERM and SIGINT --------- Co-authored-by: Jay Rogers <[email protected]>
1 parent d1f786d commit c248913

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/entrypoint.sh

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/sh
22

3+
# Listen for "docker stop": https://superuser.com/a/1299463/57662
4+
# shellcheck disable=SC3048
5+
trap "echo Shutdown requested; exit 0" SIGTERM
6+
37
# Permissions must be created after volumes have been mounted; otherwise, windows file system permissions will override
48
# the permissions set within the container.
59
mkdir -p /etc/letsencrypt/accounts /var/log/letsencrypt /var/lib/letsencrypt
@@ -86,14 +90,30 @@ replace_symlinks() {
8690
done
8791
}
8892

93+
cleanup() {
94+
echo "Shutdown requested, exiting gracefully..."
95+
exit 0
96+
}
97+
98+
trap cleanup SIGTERM SIGINT
99+
89100
# Run certbot initially
90101
run_certbot
91102

92103
# Infinite loop to keep the container running and periodically check for renewals
93104
while true; do
94105
next_run=$(date -d "@$(($(date +%s) + RENEWAL_INTERVAL))" '+%Y-%m-%d %H:%M:%S')
95106
echo "Next certificate renewal check will be at ${next_run}"
96-
sleep "$RENEWAL_INTERVAL"
107+
108+
# Use wait with timeout to allow for signal interruption
109+
sleep $RENEWAL_INTERVAL &
110+
wait $!
111+
112+
# Check if we received a signal
113+
if [ $? -gt 128 ]; then
114+
cleanup
115+
fi
116+
97117
if ! run_certbot; then
98118
echo "Error: Certificate renewal failed. Exiting."
99119
exit 1

0 commit comments

Comments
 (0)