Automatic Reboot on Kernel Update

Latest response

Please can anyone suggest the best way to automatically reboot a server when I new kernel is installed on RHEL 7?

I am using yum-cron to automatically apply updates which I've scheduled to run using crontab.


Without looking into it too closely, i'd probably write a cron script to compare the currently running kernel (with uname) against the default selected kernel in grub (which is modified on kernel package install) or latest installed kernel rpm package (with rpm --last).

If they differ, reboot.

Would probably need some testing to handle edge cases too, eg. kernel downgrades.

Automatic reboot would not be a good option for me, especially in production environment. If your setup permits that and could incur any damage that would come across then you may do it. Yes, as "PixelDrift" suggested it would need a cron job to be setup.

Thanks for the input, this is the script I have decided to use. Nice and simple but does the job.

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

test $LAST_KERNEL = $CURRENT_KERNEL || shutdown -r

Taken from this thread

Don't forget that you might need to reboot because of core library updates, at least if it is glibc. (And also, services may need to be restarted after updates).

If you install the yum-utils package, you can use a command called needs-restarting.

You can use it both for checking if a full reboot is required because of kernel or core libraries updates (using the -r option), or what services need to be restarted (using the -s option.

needs-restarting -r returns 0 if reboot is not needed, and 1 if it is, so it is perfect to use in a script.

An example:

root@server1:~> needs-restarting  -r ; echo $?
Core libraries or services have been updated:
  openssl-libs -> 1:1.0.1e-60.el7_3.1
  systemd -> 219-30.el7_3.9

Reboot is required to ensure that your system benefits from these updates.

More information:

Great response Terje, thanks for posting.

I haven't come across needs-restarting before.. will take a look!

I'm keen to pull it apart to see how it determines the reboot state etc. will be very handy for one of my customer requirements.

Thanks again.

Thanks very much. I've just tested this and it does exactly what I need so I will now be using this to automate my restarts.

needs-restarting -r || shutdown -r

How to avoid reboot while yum may be updating packages? Is needs-restarting claiming lock for yum data? This would be safe enough if reboot is issued instantly after querying reboot need with needs-restarting.

Hi Tom,

Interesting question.

I deliberately ran a yum install session and in another window ran "needs-restarting -r". It appears that needs-restarting did not check the yum lock.


Dusan Baljevic (amateur radio VK2COT)

If you are worried about yum running whilst reboot is underway, maybe a simple test:

$ test ! -f /var/run/ && echo "Can reboot"


Dusan Baljevic (amateur radio VK2COT)

And note that on RHEL 8 there are no locks when using DNF (versus older YUM).

With DNF one will never face "another app is currently holding the lock" as DNF does not rely on any PID file and multiple instances of DNF can be executed in parallel.

So, on RHEL 8, possibly something simple as this would suffice:

$ test ! pgrep dnf && echo "Can reboot"


Dusan Baljevic (amateur radio VK2COT)