bin/check-reboot
2023-05-18 09:59:56 -05:00

39 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
HOSTS_FILE="${HOME}/.server_hosts"
host_array=()
YELLOW='\033[1;33m'
GREEN='\033[1;32m'
NC='\033[0m'
# Read hosts from ~/.server_hosts
while IFS= read -r line; do
host_array+=("$line")
done < "${HOSTS_FILE}"
for host in "${host_array[@]}"; do
hostname=$(echo "$host" | cut --delimiter="@" --fields=2)
if ssh "$host" -- test -f /etc/redhat-release; then
if ssh "$host" -- sudo needs-restarting -r >/dev/null; then
echo -e "$hostname: ${GREEN}OK${NC}"
else
ssh "$host" -- sudo systemctl reboot >/dev/null
echo -e "$hostname: ${YELLOW}Reboot initiated${NC}"
notify-send "$hostname: Reboot initiated"
fi
elif ssh "$host" -- test -f /etc/debian_version; then
if ssh "$host" -- ! test -f /var/run/reboot-required; then
echo -e "$hostname: ${GREEN}OK${NC}"
else
ssh "$host" -- sudo systemctl reboot >/dev/null
echo -e "$hostname: ${YELLOW}Reboot initiated${NC}"
notify-send "$hostname: Reboot initiated"
fi
else
echo "$hostname is not a Debian/Ubuntu or RHEL-compatible system."
echo "Reboot check aborted"
fi
done