CentOS 7: Auto yum update

This article will describe auto yum update with yum-updateonboot and yum-cron.

 

1 Packages for auto yum update

CentOS has two packages for auto yum update.

 

yum-updateonboot Run yum update at once on boot
yum-cron Run yum update periodically

 

yum-updateonboot should be used in case of desktop and yum-cron should be used in case of server.

2 yum-updateonboot

This package uses systemd for auto yum udpate.

Install yum-updateonboot with yum and enable yum-updateonboot with systemctl.

$ sudo yum install -y yum-updateonboot
$ sudo systemctl enable yum-updateonboot

auto yum update will be run on next boot.

3 yum-cron

This package uses cron and anacron for auto yum update.

Install yum-cron with yum and enable yum-cron with systemctl.

$ sudo yum install -y yum-cron
$ sudo systemctl enable yum-cron
$ sudo systemctl start yum-cron

yum-cron have 0yum-hourly.cron for hourly running and 0yum-daily.cron for daily running.

 

cron job file config file for yum update
/etc/cron.hourly/0yum-hourly.cron /etc/yum/yum-cron-hourly.conf
/etc/cron.daily/0yum-daily.cron /etc/yum/yum-cron.conf

 

By default, 0-yum-hourly.cron does nothing and 0-yum-daily.cron download updated package only.

 

This article will change 0-yum-daily.cron to be able to update.

And change random sleep time for start yum update to be 0 because anacron has already waited a random time for start a cron job.

$ diff -uprN /etc/yum/yum-cron.conf{.org,}
--- /etc/yum/yum-cron.conf.org  2016-05-26 21:01:11.352642602 +0900
+++ /etc/yum/yum-cron.conf      2016-05-26 21:01:30.218747972 +0900
@@ -17,7 +17,7 @@ download_updates = yes

 # Whether updates should be applied when they are available.  Note
 # that download_updates must also be yes for the update to be applied.
-apply_updates = no
+apply_updates = yes

 # Maximum amout of time to randomly sleep, in minutes.  The program
 # will sleep for a random amount of time between 0 and random_sleep
@@ -25,7 +25,7 @@ apply_updates = no
 # times that multiple systems will access update servers.  If
 # random_sleep is 0 or negative, the program will run immediately.
 # 6*60 = 360
-random_sleep = 360
+random_sleep = 0


 [emitters]

3.1 Execution result

0anacron started cron.daily, and cron.daily started 0yum-daily.cron.

yum update took 5 minites. 0yum-hourly.cron was started but did not do nothing.

May 27 20:01:01 centos-7 CROND[2529]: (root) CMD (run-parts /etc/cron.hourly)
May 27 20:01:01 centos-7 run-parts(/etc/cron.hourly)[2529]: starting 0anacron
May 27 20:01:01 centos-7 anacron[2552]: Anacron started on 2016-05-27
May 27 20:01:01 centos-7 run-parts(/etc/cron.hourly)[2555]: finished 0anacron
May 27 20:01:01 centos-7 anacron[2552]: Will run job `cron.daily' in 44 min.
May 27 20:01:01 centos-7 anacron[2552]: Will run job `cron.weekly' in 64 min.
May 27 20:01:01 centos-7 anacron[2552]: Will run job `cron.monthly' in 84 min.
May 27 20:01:01 centos-7 anacron[2552]: Jobs will be executed sequentially
May 27 20:01:01 centos-7 run-parts(/etc/cron.hourly)[2529]: starting 0yum-hourly.cron
May 27 20:01:14 centos-7 run-parts(/etc/cron.hourly)[2604]: finished 0yum-hourly.cron
May 27 20:45:01 centos-7 anacron[2552]: Job `cron.daily' started
May 27 20:45:01 centos-7 run-parts(/etc/cron.daily)[5838]: starting 0yum-daily.cron
May 27 20:51:00 centos-7 run-parts(/etc/cron.daily)[30186]: finished 0yum-daily.cron
May 27 20:51:00 centos-7 run-parts(/etc/cron.daily)[5838]: starting logrotate
May 27 20:51:00 centos-7 run-parts(/etc/cron.daily)[30209]: finished logrotate
May 27 20:51:00 centos-7 run-parts(/etc/cron.daily)[5838]: starting man-db.cron
May 27 20:52:00 centos-7 run-parts(/etc/cron.daily)[5493]: finished man-db.cron
May 27 20:52:00 centos-7 run-parts(/etc/cron.daily)[5838]: starting mlocate
May 27 20:52:08 centos-7 run-parts(/etc/cron.daily)[5513]: finished mlocate
May 27 20:52:08 centos-7 anacron[2552]: Job `cron.daily' terminated (mailing output)

A log of yum update is at /var/log/yum. This log sent to mail box too.

$ cat /var/log/yum
May 27 20:45:49 Updated: java-1.8.0-openjdk-headless.x86_64 1:1.8.0.91-0.b14.el7_2
May 27 20:45:50 Updated: java-1.7.0-openjdk-headless.x86_64 1:1.7.0.101-2.6.6.1.el7_2
May 27 20:46:02 Updated: tzdata-java.noarch 2016d-1.el7
<snip>
May 27 20:48:34 Updated: crash.x86_64 7.1.2-3.el7_2.1
May 27 20:48:35 Updated: graphite2.x86_64 1.3.6-1.el7_2
May 27 20:48:51 Installed: kernel-devel.x86_64 3.10.0-327.18.2.el7

3.2 yum-cron does not support reboot

If you need auto reboot, you must create cron job for reboot.

The time of the cron job should be out of START_HOURS_RANGE range which is defined at /etc/anacrontab.

A range START_HOURS_RANGE is 3-22 by default and this article will create cron job which will run "shtudown -r 0:10" at 00:00 on Sunday.

The cron job will output log with using "shutdown -r 0:10" instead of "shutdown -r now".

$ sudo crontab -u root -l
0 0 * * 0 /usr/sbin/shutdown -r 0:10