Add check-reboot

This commit is contained in:
Jeffrey Serio 2023-05-18 09:59:56 -05:00
parent 464908347d
commit 8f4e2fd5f7
2 changed files with 38 additions and 37 deletions

38
check-reboot Executable file
View File

@ -0,0 +1,38 @@
#!/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

View File

@ -1,37 +0,0 @@
#!/usr/bin/env bash
# This script reads user@host lines from ~/.server_hosts. Each host
# is a RHEL-like operating system where the dnf-utils and
# dnf-automatic packages are installed. dnf-automatic is configured
# to download and apply updates periodically. The 'needs-restarting'
# command provided by the dnf-utils packages is assumed to be
# available on each host. Each host is checked via SSH to see if it
# needs-restarting after core libraries and services have been
# updated since the last boot-up. If a restart has been initiated for
# a given host, notify-send will notify the user.
set -euo pipefail
HOST_FILE="${HOME}/.server_hosts"
host_array=()
RED='\033[1;31m'
YELLOW='\033[1;33m'
GREEN='\033[1;32m'
NC='\033[0m'
# Read hosts from ~/.server_hosts
while IFS= read -r line; do
host=$(echo $line)
host_array+=($host)
done < "${HOST_FILE}"
for host in "${host_array[@]}"; do
hostname=$(echo $host | cut --delimiter="@" --fields=2)
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: ${RED}Restart initiated${NC}"
notify-send "$hostname: Restart initiated"
fi
done