pi-hole/advanced/Templates/pihole-FTL.service
MichaIng 81927334f2
Add native systemd service
Inspired by: https://github.com/pi-hole/pi-hole/pull/2112

A pre-start and a post-stop script are added to reduce doubled setup and cleanup code.

Since systemd services do not natively support dynamic users, test once whether capabilities are supported during install/update, and remove User=pihole otherwise.

Signed-off-by: MichaIng <micha@dietpi.com>
Co-authored-by: DL6ER <dl6er@dl6er.de>
2022-10-30 19:40:33 +01:00

109 lines
2.3 KiB
Desktop File

#!/usr/bin/env sh
### BEGIN INIT INFO
# Provides: pihole-FTL
# Required-Start: $remote_fs $syslog $network
# Required-Stop: $remote_fs $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: pihole-FTL daemon
# Description: Enable service provided by pihole-FTL daemon
### END INIT INFO
# Source utils.sh for getFTLPIDFile(), getFTLPID()
PI_HOLE_SCRIPT_DIR="/opt/pihole"
utilsfile="${PI_HOLE_SCRIPT_DIR}/utils.sh"
# shellcheck disable=SC1090
. "${utilsfile}"
is_running() {
if [ -d "/proc/${FTL_PID}" ]; then
return 0
fi
return 1
}
# Start the service
start() {
if is_running; then
echo "pihole-FTL is already running"
else
# Run pre-start script, which pre-creates all expected files with correct permissions
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 $?
else
echo "Warning: Starting pihole-FTL as root because setting capabilities is not supported on this system"
/usr/bin/pihole-FTL || exit $?
fi
echo
fi
}
# Stop the service
stop() {
if is_running; then
kill "${FTL_PID}"
for i in 1 2 3 4 5; do
if ! is_running; then
break
fi
printf "."
sleep 1
done
echo
if is_running; then
echo "Not stopped; may still be shutting down or shutdown may have failed, killing now"
kill -9 "${FTL_PID}"
else
echo "Stopped"
fi
else
echo "Not running"
fi
# Run post-stop script, which does cleanup among runtime files
sh "${PI_HOLE_SCRIPT_DIR}/pihole-FTL-poststop.sh"
echo
}
# Indicate the service status
status() {
if is_running; then
echo "[ ok ] pihole-FTL is running"
exit 0
else
echo "[ ] pihole-FTL is not running"
exit 1
fi
}
### main logic ###
# Get FTL's PID file path
FTL_PID_FILE="$(getFTLPIDFile)"
# Get FTL's current PID
FTL_PID="$(getFTLPID "${FTL_PID_FILE}")"
case "$1" in
stop)
stop
;;
status)
status
;;
start|restart|reload|condrestart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status}"
exit 1
esac
exit 0