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.

Responses

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.

#!/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 || shutdown -r

Taken from this thread https://serverfault.com/questions/122178/how-can-i-check-from-the-command-line-if-a-reboot-is-required-on-rhel-or-centos/311733#311733

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:
https://access.redhat.com/solutions/27943
1

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