Using Cron to Schedule Tasks for Certain Times or Intervals

Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

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, generate reports, check for available disk space and notify if the space is below a certain amount.

How to Use Cron and crontab - The Basics

What is a Cron Job?

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 the crontab line of a Cron job. 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 about Cron expressions.

Add a Cron Job

  1. Open a crontab for 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 is 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 crontab file of the user.

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, runs 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 runs a task in February, March, and April. 1-5 in the day of week field runs a task every weekday.

Special Cron Syntaxes

Automating systems in Cron time units follows a specific Cron schedule format. 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 init system of the distribution, but @reboot cronjobs may be useful for those 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, in the following command you can edit the crontab for the www-data user:

sudo crontab -u www-data -e

The ability to run jobs as system users is powerful. However, it is difficult 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 sends emails to the executing user by default with any output or errors to the stdout or stderr. To disable email alerts, add >/dev/null to the end of the job’s line in the crontab file.

For example, the full line would be:

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

That ignores messages sent to stdout. If your script generates an error, Cron still sends 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. The following are some quick crontab entries to get you started.

  • Run the cal-update-daily binary every day 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
    

For additional help to create Cron expressions, you can also use a Cron translator or Cron calculator to generate the appropriate syntax.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.