Scheduling Tasks With Cron On CentOS 7

  • Wednesday, January 30, 2019
  • Linux

Executing commands on a schedule is one of the core roles of a server. Whether it’s clearing caches, rotating logs, querying APIs, or simply pinging a site to see if it is available, scheduling commands is a common server administration task. There are web services and applications that can run tasks on a schedule, but every CentOS 7 server already includes a flexible and powerful scheduling tool called cron.

There are many versions of cron. The first was released in the 1970s and multiple compatible time-based schedulers have been created in the decades since. CentOS 7 uses cronie, a lightweight command scheduler that is part of the base installation. It should already be installed on your CentOS 7 virtual private server or dedicated server. You can check with:

rpm -q cronie

Cronie looks at configuration files called crontabs and runs the commands in them at the specified times. Each user, including the root user, has a crontab file. The contents of the crontab file should be edited with the crontab command. When you run crontab, it opens the file associated with the current user, so if you want to edit root’s crontab file, run the command as root or with sudo. To edit the file, run:

crontab -e

Unless you have already scheduled commands, a new crontab file is created and opened in the vim editor.

Each line of the file consists of a time and a command to run. Times are expressed in a format that can be confusing, but is straightforward and flexible once you get the hang of it.

* * * * * ping http://www.google.com

The first five fields are time codes. The fields, in order, designate:

  • The minute (0 – 59)
  • The hour (0 – 23)
  • The day of the month (1 – 31)
  • The month (1 – 12)
  • Day of the week (0 – 6)

An asterisk (*) in a time field runs the command at every valid value. The final field is the shell command to run. Let’s look at a couple of examples.

* * * * * ping http://www.google.com

This pings Google once every minute (I’m using the ping command as an example. You should not ping Google every minute).

0 0 * * * ping http://www.google.com

This pings Google at midnight every day. The initial zero indicates the first minute of the hour. The second zero indicates the first hour of the day. The other fields are asterisks, so the command is run every day of every month.

Now for a more complex example

30 6 21 2 * ping http://www.google.com

This command is run once a year at 6:30 AM on February 21. To get a full range of dates, you need to know two more things. Each field can contain a list of numbers, separated by commas (0, 15, 30, 45). And each entry can be a range, not just a single number (0–29).

To bring all of that together:

0,14,29,44 * 1-10, 1-6, * ping http://www.google.com

See if you can figure out what this means before I tell you.

The first field is minutes, and we’re telling cron to run the command every fifteen minutes on the quarter hour. There is an * in the hour field, so that command runs every hour that is compatible with the other fields. There is a range in the day of the month field, asking for the command to be run on the first to the tenth days of the month inclusive. And the range in the month field says the command should run only January to June.

As I said, it the time patterns can be difficult to get to grips with at first, but once you understand how complex schedules can be built using these five fields, you will be able to schedule any command to run at any time.

Dedicated Server Special

Take advantage of our Double RAM offer on the E3-1230v2 4 x 3.30GHz+HT server! Only $134.95 per month. Managed and Unmanaged options available at checkout.

GET STARTED