CentOS 7 Automated reboot after kernel update

When using applications like yum-cron to update the installed packages automatically there will be times when the kernel has been updated.

Instead of checking every machine if a reboot is needed I use the following cron script to automate the reboot.

The following cron script is place in /etc/cron.daily/reboot_on_kernel_update

#!/usr/bin/env bash

EMAIL_RECIPIENT="test@example.com"
LATEST_KERNEL=$(rpm -q --last kernel | perl -pe 's/^kernel-(\S+).*/$1/' | head -1)
CURRENT_KERNEL=$(uname -r)

if [ "$LATEST_KERNEL" != "$CURRENT_KERNEL" ]
then
    MAIL_BODY="The kernel has been updated from $CURRENT_KERNEL to $LATEST_KERNEL on $HOSTNAME. Because of this a reboot has been issued. // Kernel update monitor"
    mail -s "$HOSTNAME: Kernel update monitor" $EMAIL_RECIPIENT <<< $MAIL_BODY
    reboot
fi

Remember to make the script executable…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s