Skip to content

Instantly share code, notes, and snippets.

@jfeilbach
Last active April 15, 2023 18:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfeilbach/dd54c48c2fcc06bd847588d120f34138 to your computer and use it in GitHub Desktop.
Save jfeilbach/dd54c48c2fcc06bd847588d120f34138 to your computer and use it in GitHub Desktop.
check if reboot needed on RHEL or Ubuntu

RHEL

Should work on Amazon Linux as well

needs-restarting -r ; echo $?

or

needs-restarting -r || shutdown -r

or

#!/bin/bash
LAST_KERNEL=$(rpm -q --last kernel | perl -pe 's/^kernel-(\S+).*/$1/' | head -1)
CURRENT_KERNEL=$(uname -r)

test $LAST_KERNEL = $CURRENT_KERNEL || echo REBOOT

or

#!/bin/bash

LAST_KERNEL=$(rpm -q --last kernel | perl -pe 's/^kernel-(\S+).*/$1/' | head -1);
CURRENT_KERNEL=$(uname -r);

if [ $LAST_KERNEL != $CURRENT_KERNEL ]; then
    echo -e '\e[1;33mA reboot is required. Please reboot.\e[0m';
else
    echo -e '\e[1;37mNo reboot needed.\e[0m';
fi

Ubuntu

Add the following alias to ~/.bash_aliases

alias rr='if [ -f /var/run/reboot-required ]; then echo "reboot required"; else echo "No reboot needed"; fi'

@mobeigi
Copy link

mobeigi commented Jun 20, 2020

Awesome, was able to use this to check if reboot is required daily:
0 2 * * * needs-restarting -r || shutdown -r now to reboot daily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment