forked from infra/ansible
*-test.*: Add script for reboots, maybe.
Signed-off-by: James Antill <james@and.org>
This commit is contained in:
parent
29978f3192
commit
f3f9080a47
1 changed files with 57 additions and 0 deletions
57
files/scripts/reboot-decider.sh
Executable file
57
files/scripts/reboot-decider.sh
Executable file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue