#!/usr/bin/env zsh
services_to_restart=( "authelia" "php-fpm" "nginx" "syncthing" "tailscaled" "zrepl" )
if [ "$EUID" -ne 0 ]
then echo "Run this script as root"
exit 1
fi
if ! [[ -x "$(command -v checkrestart)" ]]; then
echo "Could not find dependency: checkrestart"
exit 1
fi
# Auto-restart named services
for service_to_restart in "${services_to_restart[@]}"; do
# if checkrestart produces any output lines, restart service
if [[ $(checkrestart -b -j 0 -u root "${service_to_restart}" | wc -l | sed -E 's/^[[:space:]]*//') != "0" ]]; then
echo "service ${service_to_restart} restart"
service ${service_to_restart} restart
fi
done
# List services that were not auto-restarted
if [[ $(checkrestart -b -j 0 -u root | wc -l | sed -E 's/^[[:space:]]*//') != "0" ]]; then
echo "[info] Services requiring manual restarts:"
checkrestart -b -j 0 -u root -H | sed 's/ *//' | cut -f4 -w | sed 's/^/ * /'
fi