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
Linux – Securing your important files with XFS extendend attributes
Viewed 7308 times since Wed, Jul 25, 2018
RHEL: What is "SysRq key" and how to use it
Viewed 4728 times since Sat, Jun 2, 2018
Linux - How to monitor CPU usage
Viewed 6111 times since Fri, Jun 8, 2018
Linux nslookup Command Examples for DNS Lookup
Viewed 8472 times since Sat, Sep 29, 2018
stunnel: Authentication
Viewed 8934 times since Fri, Sep 28, 2018
Turbocharge PuTTY with 12 Powerful Add-Ons – Software for Geeks #3
Viewed 14198 times since Sun, Sep 30, 2018
Linux Find Large Files
Viewed 2588 times since Mon, Oct 29, 2018
debian How to check Debian CVE status using python script
Viewed 3301 times since Sun, Sep 23, 2018
RHEL: Back-up/Replicate a partition table
Viewed 3009 times since Sun, May 27, 2018
Linux: Disks diagnostic using smartctl
Viewed 14272 times since Wed, Jul 25, 2018