#! /bin/sh -e # Simple script to maybe reboot the machine. # We want to reboot if: # # * There's a new kernel installed. # # ...also if we are running from cron, we probably only want to reboot if # there's nobody logged into the machine (Eg. the *-test.* machines, or # maybe someone debugging a staging machine). CUR_KERNEL="$(uname -r)" _DEF_KERN_PATH="$(grubby --default-kernel)" NXT_KERNEL=$(basename "$_DEF_KERN_PATH" | sed 's/^vmlinuz-//') # How many users are logged into the machine. USER_COUNT=$(who | wc -l) function err { echo "$@" >&2 } function maybe_reboot { if [ "x$CUR_KERNEL" != "x$NXT_KERNEL" ]; then reboot fi } case "$1" in info) echo "-------- Kernel Status Check --------" echo "Running: $CUR_KERNEL" if [ "x$CUR_KERNEL" != "x$NXT_KERNEL" ]; then echo " Next: $NXT_KERNEL" fi echo "-------------------------------------" echo "Users: $USER_COUNT" ;; maybe-reboot) maybe_reboot ;; maybe-cron-reboot|cron-maybe-reboot) if [ "$USER_COUNT" -eq 0 ]; then maybe_reboot fi ;; *) err "Usage: $0 info | maybe-reboot | cron-maybe-reboot" exit 1 esac