#!/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