From f713b14ba09cafa489593da07a5f3ddf66b813f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6nig?= Date: Thu, 8 Dec 2022 09:49:02 +0100 Subject: [PATCH] Cleanup if startup failed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christian König --- advanced/Templates/pihole-FTL.service | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/advanced/Templates/pihole-FTL.service b/advanced/Templates/pihole-FTL.service index 15096972..dc7649e7 100644 --- a/advanced/Templates/pihole-FTL.service +++ b/advanced/Templates/pihole-FTL.service @@ -23,6 +23,11 @@ is_running() { return 1 } +cleanup() { + # Run post-stop script, which does cleanup among runtime files + sh "${PI_HOLE_SCRIPT_DIR}/pihole-FTL-poststop.sh" +} + # Start the service start() { @@ -33,10 +38,15 @@ start() { sh "${PI_HOLE_SCRIPT_DIR}/pihole-FTL-prestart.sh" if setcap CAP_NET_BIND_SERVICE,CAP_NET_RAW,CAP_NET_ADMIN,CAP_SYS_NICE,CAP_IPC_LOCK,CAP_CHOWN+eip "/usr/bin/pihole-FTL"; then - su -s /bin/sh -c "/usr/bin/pihole-FTL" pihole || exit $? + su -s /bin/sh -c "/usr/bin/pihole-FTL" pihole || ec=$? else echo "Warning: Starting pihole-FTL as root because setting capabilities is not supported on this system" - /usr/bin/pihole-FTL || exit $? + /usr/bin/pihole-FTL || ec=$? + fi + # Cleanup if startup failed + if [ -n "${ec}" ] && [ "${ec}" != 0 ]; then + cleanup + exit $ec fi echo fi @@ -65,8 +75,7 @@ stop() { else echo "Not running" fi - # Run post-stop script, which does cleanup among runtime files - sh "${PI_HOLE_SCRIPT_DIR}/pihole-FTL-poststop.sh" + cleanup echo } @@ -84,6 +93,9 @@ status() { ### main logic ### +# catch sudden termination +trap 'cleanup; exit 1' INT HUP TERM ABRT + # Get FTL's PID file path FTL_PID_FILE="$(getFTLPIDFile)"