1
- #! /bin/bash -e
1
+ #! /bin/bash
2
2
3
3
if [ " ${PH_VERBOSE:- 0} " -gt 0 ]; then
4
4
set -x
@@ -53,11 +53,13 @@ start() {
53
53
# migrate Gravity Database if needed:
54
54
migrate_gravity
55
55
56
- # Start pihole-FTL
57
- start_ftl
56
+ # Start pihole-FTL in the background
57
+ start_ftl &
58
58
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
61
63
62
64
# If we are migrating from v5 to v6, we now need to run the basic configuration step that we deferred earlier
63
65
# This is because pihole-FTL needs to migrate the config files before we can perform the basic configuration checks
@@ -75,8 +77,8 @@ start() {
75
77
# Start tailing the FTL log from the most recent "FTL Started" message
76
78
# Get the line number
77
79
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 &
80
82
else
81
83
echo " [i] FTL log output is disabled. Remove the Environment variable TAIL_FTL_LOG, or set it to 1 to enable FTL log output."
82
84
fi
@@ -86,22 +88,51 @@ start() {
86
88
}
87
89
88
90
stop () {
89
- # Ensure pihole-FTL shuts down cleanly on SIGTERM/SIGINT
90
- ftl_pid=$( pgrep pihole-FTL)
91
- killall --signal 15 pihole-FTL
92
91
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
97
108
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
100
130
if [[ ${PYTEST} ]]; then
101
131
sleep 10
102
132
fi
103
133
104
- exit
134
+ exit ${FTL_EXIT_CODE}
135
+
105
136
}
106
137
107
138
start
0 commit comments