Skip to content

Commit 36bbaae

Browse files
authored
Merge pull request #1940 from pi-hole/improve_startup
Improve FTL startup detection and log tailing
2 parents e23e759 + db12aa2 commit 36bbaae

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

src/start.sh

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ start() {
5858
fix_capabilities
5959
sh /opt/pihole/pihole-FTL-prestart.sh
6060

61+
# Get the FTL log file path from the config
62+
FTLlogFile=$(getFTLConfigValue files.log.ftl)
63+
64+
# Get the EOF position of the FTL log file so that we can tail from there later.
65+
local startFrom
66+
startFrom=$(stat -c%s "${FTLlogFile}")
67+
6168
echo " [i] Starting pihole-FTL ($FTL_CMD) as ${DNSMASQ_USER}"
6269
echo ""
6370

@@ -70,18 +77,11 @@ start() {
7077
# We need the PID of the capsh process so that we can wait for it to finish
7178
CAPSH_PID=$!
7279

73-
# Get the FTL log file path from the config
74-
FTLlogFile=$(getFTLConfigValue files.log.ftl)
75-
76-
# Wait until the log file exists before continuing
77-
while [ ! -f "${FTLlogFile}" ]; do
78-
sleep 0.5
79-
done
80-
81-
# Wait until the FTL log contains the "FTL started" message before continuing
82-
while ! grep -q '########## FTL started' "${FTLlogFile}"; do
83-
sleep 0.5
84-
done
80+
# Wait for FTL to start by monitoring the FTL log file for the "FTL started" line
81+
if ! timeout 30 tail -F -c +$((startFrom + 1)) -- "${FTLlogFile}" | grep -q '########## FTL started'; then
82+
echo " [!] ERROR: Did not find 'FTL started' message in ${FTLlogFile} in 30 seconds, stopping container"
83+
exit 1
84+
fi
8585

8686
pihole updatechecker
8787
local versionsOutput
@@ -91,11 +91,8 @@ start() {
9191
echo ""
9292

9393
if [ "${TAIL_FTL_LOG:-1}" -eq 1 ]; then
94-
# Start tailing the FTL log from the most recent "FTL Started" message
95-
# Get the line number
96-
startFrom=$(grep -n '########## FTL started' "${FTLlogFile}" | tail -1 | cut -d: -f1)
97-
# Start the tail from the line number and background it
98-
tail --follow=name -n +"${startFrom}" "${FTLlogFile}" &
94+
# Start tailing the FTL log file from the EOF position we recorded on container start
95+
tail -F -c +$((startFrom + 1)) -- "${FTLlogFile}" &
9996
else
10097
echo " [i] FTL log output is disabled. Remove the Environment variable TAIL_FTL_LOG, or set it to 1 to enable FTL log output."
10198
fi
@@ -104,7 +101,6 @@ start() {
104101
wait $CAPSH_PID
105102
FTL_EXIT_CODE=$?
106103

107-
108104
# If we are here, then FTL has exited.
109105
# If the trap was triggered, then stop will have already been called
110106
if [ $TRAP_TRIGGERED -eq 0 ]; then

0 commit comments

Comments
 (0)