1
0
mirror of https://github.com/pi-hole/pi-hole synced 2024-11-17 21:59:55 +00:00
pi-hole/advanced/Templates/pihole-FTL.service

120 lines
3.0 KiB
SYSTEMD
Raw Normal View History

#!/usr/bin/env bash
2017-02-21 10:18:47 +00:00
### BEGIN INIT INFO
# Provides: pihole-FTL
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# 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
FTLUSER=pihole
2017-02-21 10:18:47 +00:00
PIDFILE=/var/run/pihole-FTL.pid
get_pid() {
# First, try to obtain PID from PIDFILE
if [ -s "${PIDFILE}" ]; then
cat "${PIDFILE}"
return
fi
# If the PIDFILE is empty or not available, obtain the PID using pidof
pidof "pihole-FTL" | awk '{print $(NF)}'
2017-02-21 10:18:47 +00:00
}
is_running() {
ps "$(get_pid)" > /dev/null 2>&1
2017-02-21 10:18:47 +00:00
}
2017-02-21 10:18:47 +00:00
# Start the service
start() {
if is_running; then
echo "pihole-FTL is already running"
else
# Touch files to ensure they exist (create if non-existing, preserve if existing)
touch /var/log/pihole-FTL.log /var/log/pihole.log
touch /run/pihole-FTL.pid /run/pihole-FTL.port
touch /etc/pihole/dhcp.leases
mkdir -p /var/run/pihole
mkdir -p /var/log/pihole
chown pihole:pihole /var/run/pihole /var/log/pihole
# Remove possible leftovers from previous pihole-FTL processes
rm -f /dev/shm/FTL-* 2> /dev/null
rm /var/run/pihole/FTL.sock 2> /dev/null
# Ensure that permissions are set so that pihole-FTL can edit all necessary files
chown pihole:pihole /run/pihole-FTL.pid /run/pihole-FTL.port
chown pihole:pihole /etc/pihole /etc/pihole/dhcp.leases 2> /dev/null
chown pihole:pihole /var/log/pihole-FTL.log /var/log/pihole.log
chmod 0644 /var/log/pihole-FTL.log /run/pihole-FTL.pid /run/pihole-FTL.port /var/log/pihole.log
# Chown database files to the user FTL runs as. We ignore errors as the files may not (yet) exist
chown pihole:pihole /etc/pihole/pihole-FTL.db /etc/pihole/gravity.db 2> /dev/null
if setcap CAP_NET_BIND_SERVICE,CAP_NET_RAW,CAP_NET_ADMIN+eip "$(which pihole-FTL)"; then
su -s /bin/sh -c "/usr/bin/pihole-FTL" "$FTLUSER"
else
echo "Warning: Starting pihole-FTL as root because setting capabilities is not supported on this system"
pihole-FTL
fi
2017-02-21 10:18:47 +00:00
echo
fi
}
# Stop the service
stop() {
if is_running; then
2017-04-12 21:13:18 +00:00
kill "$(get_pid)"
for i in {1..5}; do
2017-02-21 10:18:47 +00:00
if ! is_running; then
break
fi
echo -n "."
sleep 1
done
echo
if is_running; then
2017-04-12 21:13:18 +00:00
echo "Not stopped; may still be shutting down or shutdown may have failed, killing now"
kill -9 "$(get_pid)"
2017-02-21 10:18:47 +00:00
exit 1
else
echo "Stopped"
fi
else
echo "Not running"
fi
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
}
2017-02-21 10:18:47 +00:00
### main logic ###
case "$1" in
stop)
stop
;;
status)
status
2017-02-21 10:18:47 +00:00
;;
2017-04-12 21:13:18 +00:00
start|restart|reload|condrestart)
2017-02-21 10:18:47 +00:00
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|status}"
exit 1
esac
exit 0