Automatic Reboot on Kernel Update
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.
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
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"
Regards,
Dusan Baljevic (amateur radio VK2COT)