How to use yum-cron to automatically update RHEL/CentOS Linux 6.x / 7.x

The yum command line tool is used to install and update software packages under RHEL / CentOS Linux server. I know how to apply updates using yum update command line, but I would like to use cron to update packages where appropriate manually. How do I configure yum to install software patches/updates automatically with cron?

You need to install yum-cron package. It provides files needed to run yum updates as a cron job. Install this package if you want auto yum updates nightly via cron.

 

 

 

How to install yum cron on a CentOS/RHEL 6.x/7.x

Type the following yum command on:
$ sudo yum install yum-cron
How to install yum-cron on CentOS RHEL server
Turn on service using systemctl command on CentOS/RHEL 7.x:
$ sudo systemctl enable yum-cron.service
$ sudo systemctl start yum-cron.service
$ sudo systemctl status yum-cron.service

If you are using CentOS/RHEL 6.x, run:
$ sudo chkconfig yum-cron on
$ sudo service yum-cron start

How to turn on yum-cron-service on CentOS or RHEL server
yum-cron is an alternate interface to yum. Very convenient way to call yum from cron. It provides methods to keep repository metadata up to date, and to check for, download, and apply updates. Rather than accepting many different command line arguments, the different functions of yum-cron can be accessed through config files.

How to configure yum-cron to automatically update RHEL/CentOS Linux

You need to edit /etc/yum/yum-cron.conf and /etc/yum/yum-cron-hourly.conf files using a text editor such as vi command:
$ sudo vi /etc/yum/yum-cron.conf
Make sure updates should be applied when they are available
apply_updates = yes
You can set the address to send email messages from. Please note that ‘localhost’ will be replaced with the value of system_name.
email_from = root@localhost
List of addresses to send messages to.
email_to = your-it-support@some-domain-name
Name of the host to connect to to send email messages.
email_host = localhost
If you do not want to update kernel package add the following on CentOS/RHEL 7.x:
exclude=kernel*
For RHEL/CentOS 6.x add the following to exclude kernel package from updating:
YUM_PARAMETER=kernel*
Save and close the file in vi/vim. You also need to update /etc/yum/yum-cron-hourly.conf file if you want to apply update hourly. Otherwise /etc/yum/yum-cron.conf will run on daily using the following cron job (us cat command:
$ cat /etc/cron.daily/0yum-daily.cron
Sample outputs:

#!/bin/bash
 
# Only run if this flag is set. The flag is created by the yum-cron init
# script when the service is started -- this allows one to use chkconfig and
# the standard "service stop|start" commands to enable or disable yum-cron.
if [[ ! -f /var/lock/subsys/yum-cron ]]; then
  exit 0
fi
 
# Action!
exec /usr/sbin/yum-cron /etc/yum/yum-cron-hourly.conf
[root@centos7-box yum]# cat /etc/cron.daily/0yum-daily.cron
#!/bin/bash
 
# Only run if this flag is set. The flag is created by the yum-cron init
# script when the service is started -- this allows one to use chkconfig and
# the standard "service stop|start" commands to enable or disable yum-cron.
if [[ ! -f /var/lock/subsys/yum-cron ]]; then
  exit 0
fi
 
# Action!
exec /usr/sbin/yum-cron

That is all. Now your system will update automatically everyday using yum-cron. See man page of yum-cron for more details:
$ man yum-cron

 

 

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

https://access.redhat.com/solutions/10021

 

0 (0)
Article Rating (No Votes)
Rate this article
Attachments
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
Tips to Solve Linux & Unix Systems Hard Disk Problems
Viewed 4229 times since Fri, May 15, 2020
How to remove CTRL-M (^M) characters from a file in Linux
Viewed 2693 times since Thu, Feb 7, 2019
stunnel How To Set Up an SSL Tunnel Using Stunnel on Ubuntu
Viewed 1841 times since Sun, Dec 6, 2020
LUKS dm-crypt/Device encryption GUIDE
Viewed 2371 times since Fri, Jul 13, 2018
debian How to Upgrade Debian 8 Jessie to Debian 9 Stretch
Viewed 2401 times since Sun, Sep 23, 2018
LVM: Extend an existing Volume Group by adding a new disk
Viewed 2140 times since Sat, Jun 2, 2018
Prosty skaner portów TCP w bash
Viewed 3396 times since Thu, May 24, 2018
How to use yum-cron to automatically update RHEL/CentOS Linux
Viewed 2733 times since Wed, Oct 17, 2018
Installing and Configuring an OCFS2 Clustered File System
Viewed 5992 times since Sat, Jun 2, 2018
ubuntu How to Reset Forgotten Root Password in Ubuntu
Viewed 2980 times since Tue, Dec 8, 2020