CentOS: Automate yum update. Reboot OS only when kernel is updated

Posted by paul on 2017.07.25


Using a small shell script and cron, you can automate updating all your CentOS servers with latest hot fixes while minimizing unnecessary OS reboots.

Automating "yum update" on CentOS

Updating CentOS servers with latest hot fixes is easily done by running "yum -y update". However you sometimes need to reboot the OS when kernel is updated. After checking logs on my server from past 10 yum update operations (twice a week), I noticed kernel was updated 3 times which required OS reboot. This manual reboot step is a bit of an issue if you wanted to schedule "yum -y update". I automated this reboot part with following script, yumupdate.sh.

Test yumupdate.sh

  1. Run these steps with root privilege on your CentOS 6 or 7 OS. The script will create folder /root/logsyum/ to keep logs. The log file will be named /root/logsyum/yum-update-{datestamp-timestamp}-utc.log
  2. Pull the script from https://raw.githubusercontent.com/paulcodrpub/yumupdate/master/yumupdate.sh
  3. wget https://raw.githubusercontent.com/paulcodrpub/yumupdate/master/yumupdate.sh
  4. Set execute permission.
  5. chmod 750 yumupdate.sh
  6. Run yumupdate.sh, with root privilege. If kernel was updated, the OS will reboot. If kernel was not updated, there will be no reboot.
  7. ./yumupdate.sh
  8. Review log file created by yumupdate.sh. It should be in a file like below.
  9. /root/logsyum/yum-update-{datestamp-timestamp}-utc.log

Schedule cron

Use cron to run yumupdate.sh regularly to keep your servers updated.

  1. Pull the script from github.
  2. Run following steps with root privilege on your server.
  3. Keep the script in /root/bin/yumupdate.sh. Or any other folder of your preference. You should use a standardized location to keep such scripts on your servers.
  4. Set execute permission on /root/bin/yumupdate.sh
  5. Manually run /root/bin/yumupdate.sh to check.
  6. Edit /etc/crontab and add following. Adjust the time and day according to your environment.
  7. 1 22 * * 2,5 root /root/bin/yumupdate.sh
  8. Now your CentOS server will run "yum -y update" twice a week, and will reboot only when kernel is updated.