From f3f9080a4740fa582b42d9dc20d3e5f7dcb5821a Mon Sep 17 00:00:00 2001 From: James Antill Date: Tue, 18 Nov 2025 18:52:08 -0500 Subject: [PATCH] *-test.*: Add script for reboots, maybe. Signed-off-by: James Antill --- files/scripts/reboot-decider.sh | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 files/scripts/reboot-decider.sh diff --git a/files/scripts/reboot-decider.sh b/files/scripts/reboot-decider.sh new file mode 100755 index 0000000000..7b1dfe9206 --- /dev/null +++ b/files/scripts/reboot-decider.sh @@ -0,0 +1,57 @@ +#! /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