Skip to content

Commit 210a117

Browse files
committed
Always terminate the container if pihole-FTL binary exits. Either naturally or via an error. Don't attempt to restart it, allow the container's restart policy to do this.
Signed-off-by: Adam Warner <[email protected]>
1 parent 89eb7f6 commit 210a117

File tree

2 files changed

+56
-25
lines changed

2 files changed

+56
-25
lines changed

src/bash_functions.sh

+8-8
Original file line numberDiff line numberDiff line change
@@ -202,25 +202,25 @@ setup_web_password() {
202202
start_ftl() {
203203

204204
echo " [i] pihole-FTL pre-start checks"
205-
echo ""
206205

207206
# Remove possible leftovers from previous pihole-FTL processes
208207
rm -f /dev/shm/FTL-* 2>/dev/null
209208
rm -f /run/pihole/FTL.sock
210209

211-
# Is /var/run/pihole used anymore? Or is this just a hangover from old container version
212-
# /var/log sorted by running pihole-FTL-prestart.sh
213-
# mkdir -p /var/run/pihole /var/log/pihole
214-
# touch /var/log/pihole/FTL.log /var/log/pihole/pihole.log
215-
# chown -R pihole:pihole /var/run/pihole /var/log/pihole /etc/pihole
216-
217210
fix_capabilities
218211
sh /opt/pihole/pihole-FTL-prestart.sh
219212

220213
echo " [i] Starting pihole-FTL ($FTL_CMD) as ${DNSMASQ_USER}"
221-
capsh --user=$DNSMASQ_USER --keep=1 -- -c "/usr/bin/pihole-FTL $FTL_CMD >/dev/null" &
222214
echo ""
223215

216+
capsh --user=$DNSMASQ_USER --keep=1 -- -c "/usr/bin/pihole-FTL $FTL_CMD >/dev/null"
217+
ftl_exit_code=$?
218+
# Store the exit code so that we can exit the container with the same code from start.sh
219+
echo $ftl_exit_code >/pihole-FTL.exit
220+
221+
# pihole-FTL has exited, so we should stop the rest of the container
222+
killall --signal 15 start.sh
223+
224224
# Notes on above:
225225
# - DNSMASQ_USER default of pihole is in Dockerfile & can be overwritten by runtime container env
226226
# - /var/log/pihole/pihole*.log has FTL's output that no-daemon would normally print in FG too

src/start.sh

+48-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash -e
1+
#!/bin/bash
22

33
if [ "${PH_VERBOSE:-0}" -gt 0 ]; then
44
set -x
@@ -53,11 +53,13 @@ start() {
5353
#migrate Gravity Database if needed:
5454
migrate_gravity
5555

56-
# Start pihole-FTL
57-
start_ftl
56+
# Start pihole-FTL in the background
57+
start_ftl &
5858

59-
# Give FTL a couple of seconds to start up
60-
sleep 2
59+
# Wait until the log file exists before continuing
60+
while [ ! -f /var/log/pihole/FTL.log ]; do
61+
sleep 0.5
62+
done
6163

6264
# If we are migrating from v5 to v6, we now need to run the basic configuration step that we deferred earlier
6365
# This is because pihole-FTL needs to migrate the config files before we can perform the basic configuration checks
@@ -75,8 +77,8 @@ start() {
7577
# Start tailing the FTL log from the most recent "FTL Started" message
7678
# Get the line number
7779
startFrom=$(grep -n '########## FTL started' /var/log/pihole/FTL.log | tail -1 | cut -d: -f1)
78-
# Start the tail from the line number
79-
tail -f -n +${startFrom} /var/log/pihole/FTL.log &
80+
# Start the tail from the line number and background it
81+
tail --follow=name -n +${startFrom} /var/log/pihole/FTL.log &
8082
else
8183
echo " [i] FTL log output is disabled. Remove the Environment variable TAIL_FTL_LOG, or set it to 1 to enable FTL log output."
8284
fi
@@ -86,22 +88,51 @@ start() {
8688
}
8789

8890
stop() {
89-
# Ensure pihole-FTL shuts down cleanly on SIGTERM/SIGINT
90-
ftl_pid=$(pgrep pihole-FTL)
91-
killall --signal 15 pihole-FTL
9291

93-
# Wait for pihole-FTL to exit
94-
while test -d /proc/"${ftl_pid}"; do
95-
sleep 0.5
96-
done
92+
# Only attempt to close pihole-FTL if it is running, it may already have crashed
93+
if pgrep pihole-FTL >/dev/null; then
94+
echo ""
95+
echo " [i] Container stop requested..."
96+
echo " [i] pihole-FTL is running - Attempting to shut it down cleanly"
97+
echo ""
98+
# Ensure pihole-FTL shuts down cleanly on SIGTERM/SIGINT
99+
ftl_pid=$(pgrep pihole-FTL)
100+
101+
killall --signal 15 pihole-FTL
102+
103+
# Wait for pihole-FTL to exit
104+
while test -d /proc/"${ftl_pid}"; do
105+
sleep 0.5
106+
done
107+
fi
97108

98-
# If we are running pytest, keep the container alive for a little longer
99-
# to allow the tests to complete
109+
# Wait for a few seconds to allow the FTL log tail to catch up before exiting the container
110+
sleep 2
111+
112+
# read the FTL exit code from the file created in the `start_ftl` function
113+
FTL_EXIT_CODE=$(cat /pihole-FTL.exit)
114+
rm /pihole-FTL.exit
115+
116+
# ensure the exit code is an integer, if not set it to 1
117+
if ! [[ "${FTL_EXIT_CODE}" =~ ^[0-9]+$ ]]; then
118+
FTL_EXIT_CODE=1
119+
fi
120+
121+
echo ""
122+
echo " [i] pihole-FTL exited with status $FTL_EXIT_CODE"
123+
echo ""
124+
echo " [i] Container will now stop or restart depending on your restart policy"
125+
echo " https://docs.docker.com/engine/containers/start-containers-automatically/#use-a-restart-policy"
126+
echo ""
127+
128+
# # If we are running pytest, keep the container alive for a little longer
129+
# # to allow the tests to complete
100130
if [[ ${PYTEST} ]]; then
101131
sleep 10
102132
fi
103133

104-
exit
134+
exit ${FTL_EXIT_CODE}
135+
105136
}
106137

107138
start

0 commit comments

Comments
 (0)