Schedule Tasks with Cron

Updated by Linode Written by Linode

Contribute on GitHub

Report an Issue | View File | Edit File

Schedule Tasks with Cron

What is Cron?

Cron is a classic utility found on Linux and UNIX systems for running tasks at pre-determined times or intervals. These tasks are referred to as Cron tasks or Cron jobs. Use Cron to schedule automated updates, report generation, or check for available disk space every day and send you an email if it falls below a certain amount.

How to Use Cron and crontab - The Basics

System Cron jobs exist as entries in the /etc/crontab file. Each job is described on a single line by defining a time interval, a user to run the command as, and the command to run. Cron can run any kind of script, command, or executable.

Below is the default system crontab file from Debian 9:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /        cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /        cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

The first job in the Cron table is:

`17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly`.

This means at 17 minutes past each hour, change directory to /, the root of the filesystem. Then, as the root user, run the run-parts binary to execute all jobs in /etc/cron.hourly.

Time intervals are denoted by numbers and operators filled in place of each asterisk in a Cron job’s crontab line. From left to right, the asterisks represent:

  • Minutes specified as a number from 0 to 59.
  • Hours specified as numbers from 0 to 23.
  • Days of the month, specified as numbers from 1 to 31.
  • Months specified as numbers from 1 to 12.
  • Days of the week, specified as numbers from 0 to 7, with Sunday represented as either/both 0 and 7.

See man crontab for more information.

Add a Cron Job

  1. Open a crontab for your user in a text editor (vi in most distributions):

    crontab -e
    
    Note

    To change the text editor used, add the environment variable to your ~/.bashrc file, exchanging vim for nano, or whatever other terminal-based editor you prefer.

    export EDITOR=vim
    
  2. Add the Cron job, save, and exit. The crontab will be saved in /var/spool/cron/crontabsas a crontab specific to the user who created it. To later remove a Cron job from it, delete the line from the user’s crontab file.

Special Cron Operators

Cron has additional operators to specify more complex time intervals. They are:

  • / operator: “steps through” or “skips” specified units. Therefore */3 in the hour field, will run the specified job, at 12:00am, 3:00am, 6:00am, 9:00am, 12:00pm, 3:00pm, 6:00pm, and 9:00pm. A */3 in the “day of month” field, runs the given task on the 3rd, 6th, 9th, 12th, 15th, 18th, 21st, and 29th of every month.
  • , operator: allows you to specify a list of times for repetition. Comma separated lists of times must not contain a space.
  • - operator: specifies a range of values. 2-4 in the month field will run a task in February, March, and April. 1-5 in the day of week field will run a task every weekday.

Special Cron Syntaxes

There are a number of special Cron schedule shortcuts used to specify common intervals. These are specified on the crontab entry in place of the conventional five column date specification. These special interval statements are:

  • @yearly and @annually both run the specified task every year at 12:00am on the 1st of January. This is equivalent to specifying 0 0 1 1 * in the crontab line.
  • @daily and @midnight both run the cronjob every day at 12:00am. This is equivalent to the following cron syntax: 0 0 * * *.
  • @monthly runs the job once a month, on the 1st, at 12:00am. In standard cron syntax this is equivalent to: 0 0 1 * *.
  • @weekly runs the job once a week at 12:00am on Sunday. This is the same as specifying 0 0 * * 0 on the crontab line.
  • @hourly runs the job at the top of every hour. In standard cron syntax this is equivalent to: 0 * * * *.
  • The @reboot statement runs the specified command once, at start up. Generally boot-time tasks are managed by the distribution’s init system, but @reboot cronjobs may be useful for users who don’t have access to edit systemd units or other init scripts.

Run Jobs as Other Users

Cron can run tasks as other system users than just root. This is useful if you want to restrict the ability of a script to write to certain locations. For example, the command below allows you to edit the crontab for the www-data user:

sudo crontab -u www-data -e

While the ability to run jobs as system users is powerful, it can sometimes be confusing to manage a large number of crontab files dispersed among many system users. Also carefully consider the security implications of running a cronjob with more privileges than is required.

Redirect Cron Job Messages

Cron will email the executing user by default with any output or errors that would normally be sent to the stdout or stderr. To disable email alerts, add >/dev/null to the end of the job’s line in your crontab file.

For example, the full line would be:

@hourly /opt/bin/job >/dev/null

That will only ignore messages sent to stdout. If your script generates an error, Cron will still send it to your email.

If you want to disable all output, including error messages, use >/dev/null 2>&1 instead. Be aware that redirecting all output to /dev/null causes you to miss important errors if something goes wrong. For example:

@hourly /opt/bin/job >/dev/null 2>&1

Example crontab Entries

The site crontab.guru has a large number of Cron job examples. Below are some quick crontab entries to get you started.

  • Run the cal-update-daily binary every day at at 12:01am (1 0).

    1 0 \* \* \* /opt/bin/cal-update-daily
    
  • Run the /opt/bin/payroll-bi-monthly application at 4:45pm (45 16), on the 1st and 15th of every month (1,15).

    45 16 1,15 \* \* /opt/bin/payroll-bi-monthly
    
  • Run the compress-static-files script at the beginning of every hour. This can be done in two different ways. Enter only one into your crontab file.

    Option A

    0 \* \* \* \* /opt/bin/compress-static-files
    

    Option B

    @hourly /opt/bin/compress-static-files
    

Join our Community

Find answers, ask questions, and help others.

comments powered by Disqus

This guide is published under a CC BY-ND 4.0 license.