5

I want to set up up yum to auto update the system using yum-cron. But my internet connection is very limited and I don't want the update process to hog the tiny bit of internet that is available and make the computer usage for everybody on the network miserable.

How can I set up yum to check and download updates automatically, but only between 2am and 6am?

6

Well, if it were me, I would set up a cron job (for root) that starts at 2am every day. Like so:

0 2 * * * /bin/yum -y update

It's about as KISS as it can get!

4

Here's a short answer. Run the following command as the root user:

cat <<HEREDOC > /etc/crontab.d/update-yum.crontab
SHELL=/bin/bash 
PATH=/sbin:/bin:/usr/sbin:/usr/bin 
MAILTO=root
HOME=/
0 2 * * *  yum -y update
HEREDOC

A detailed explanation follows.


There are several utilities for running scheduled jobs on Unix systems:

The cron utility is the de facto standard Unix utility for performing scheduled tasks. One disadvantage of cron is that the task may not be performed if the system is down for some reason

The anacron utility was created for use cases in which the host system may not be up continuously:

anacron is a computer program that performs periodic command scheduling, which is traditionally done by cron, but without assuming that the system is running continuously.

The fcron utility is a newer version that is also designed to handle this kind of situation:

fcron is a computer program [...] performs periodic command scheduling. [fcron] does not assume that the system is running continuously, and can run in systems that do not run all the time or regularly. It aims to replace Vixie-cron and Anacron with a single integrated program, providing many features missing from the original Cron daemon.

The hcron utility is a much more feature-rich alternative:

hcron [brings something new to the table] in a number of really useful and practical ways. For example:

  • events are stored individually, each in their own file, rather than all in a single file
  • events are organized hierarchically in the filesystem rather than as a table in a single file
  • events are named and referenceable
  • events are defined as multiple key=value settings rather than a ordered columns on a single line
  • hcron is network oriented rather than system oriented
  • support for template events to reducing and reuse settings
  • support for failover events if an event cannot be spawned
  • support for settings and working with variables

If you don't need the additional features of any of the other variants then the simplest to use would be cron. The typical workflow for cron is to create a so-called cron job for a given task, which consists of a command to run and a pattern which specifies when to run it. The syntax for the time pattern is given by the following:

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday;
# │ │ │ │ │                                       7 is also Sunday on some systems)
# │ │ │ │ │
# │ │ │ │ │
# * * * * *  command to execute

The cron daemon runs every minute and checks all active cron jobs to see if any of the jobs have patterns which match the current time. For example, to run a job at 2:00AM you would want the minute value to be 0, the hour value to be 2, and the day-of-month, month, and day-of-week values to be unrestricted. This would be denoted as follows:

0 2 * * * command

The syntax might take a little getting used to, so here's a web-based utility which will help you generate cron job expressions:

And here's another web-based tool that you might find useful:

It does the reverse - it allows you to enter a time pattern and it displays it in English.

There are several different ways to deploy cron jobs. There is a main crontab file and several subdirectories of /etc/ which are intended for system cron jobs:

/etc/crontab
/etc/cron.d
/etc/cron.daily
/etc/cron.deny
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly

For more information about this see, for instance, the relevant section from the CentOS Deployment Guide:

The easiest thing to do might be to add a crontab file to the /etc/crontab.d directory, e.g.

cat <<HEREDOC > /etc/crontab.d/update-yum.crontab
SHELL=/bin/bash 
PATH=/sbin:/bin:/usr/sbin:/usr/bin 
MAILTO=root
HOME=/
0 2 * * *  yum -y update
HEREDOC

You might want to set the MAILTO variable to your preferred administrative email address so that you are informed if the job fails.

You should also be aware that the cronjobs run in a different environment than your user, which is a common gotcha and source of frustration. See, for example, the following post:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.