Read in 5 minutes

last updated 

How to create and manage cron jobs

Cron is a scheduling daemon that executes tasks at specified intervals. These tasks are called cronjobs. Cron jobs are mostly used for creating periodic and recurring tasks.

For example, you could set a cron job to backup your databases or data, update your system with the latest security patches, check your disk space usage, sending emails and more. Some applications, such as Drupal or Magento requires cron jobs to perform certain functions.

You can set up cron jobs run by the minute, hour, day of the month, month, day of the week or any combination of these.

Crontab Syntax and Operators

Each line in the crontab file contains six fields separated by a space.

* * * * * command(s) to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

THe first five fields may contain one or more values, separated by a comma or a range of values separated by a hyphen.

  • * - The asterisk operator means any value or always. If you have the asterisk symbol in the Hour field it means the task will be performed each hour.
  • , - The comma operator allows you to specify a list of values for repetition. For example if you have 1,3,5 in the Hour field, the task will run at 1am, 3am and 5am.
  • - - The hyphen operator allows you to specify a range of values. If you have 1-5 in the Day of week field the task will run every weekday (From Monday to Friday).
  • / - The slash operator allows you to specify values that will be repeated over a certain interval between them. For example if you have */4 in the Hour field it means the the action will be performed every four hours. It is same as specifying 0,4,8,12,16,20. Instead of asterisk before the slash operator you can also use a range of values. For example 1-30/10 means the same as 1,11,21.

Predefined Macros

There are a several special Cron schedule macros used to specify common intervals. You can use this shortcuts in place of the five column date specification.

  • @yearly (or @annually) - Run the specified task once a year at midnight (12:00am) of 1st of January. Equivalent to 0 0 1 1 *.
  • @monthly - Run the specified task once a month at midnight of the first day of the month. Equivalent to 0 0 1 * *.
  • @weekly - Run the specified task nce a week at midnight on Sunday. Equivalent to 0 0 * * 0.
  • @daily - Run the specified task once a day at midnight. Equivalent to 0 0 * * *.
  • @hourly - Run the specified task once an hour at the beginning of the hour. Equivalent to 0 * * * *.
  • @reboot - Run the specified task at the system startup (boot-time).

Create and Manage Cron Jobs

You can create new cron jobs, as well as view, edit and delete existing cron jobs, using the following command:

crontab -e

Once you run the command above it will open a new screen with existing cron tasks if they exist.

Crontab files are stored in the /var/spool/cron/crontabs directory, with the user’s name as a file name. Although you can edit the file manually, it is recommended to use the crontab utility.

By default the email notification is sent to the owner of the crontab. To overwrite the default behavior you can use the MAILTO environment variable with a list (comma separated) of all the email addresses you want to receive the email notifications. If MAILTO is defined but empty (MAILTO=""), no mail is sent.

Cron Jobs Examples

Below are some examples which will show you how to schedule a cron job to run on different time periods.

  • Run a command at 15:00 on every day from Monday through Friday:

    `cronjob 0 22 * * 1-5 command

  • Run a script every 5 minutes and redirected the standard output to dev null, only the standard error will be sent to the specified e-mail address:

    [email protected]
    */5 * * * * /path/to/script.sh > /dev/null
  • Run two commands every Monday at 3 PM (use the operator && between the commands):

    0 15 * * Mon command1 && command2
  • Run a PHP script every 2 minutes and write the output to a file:

    */2 * * * * /usr/bin/php /path/to/script.php >> /var/log/script.log
  • Run a script every day, every hour, on the hour, from 8 AM through 4 PM:

    00 08-16 * * * /path/to/script.sh
  • Run a script on the first Monday of each month, at 7 a.m.

    0 7 1-7 * 1 /path/to/script.sh
  • Run the a script at 9:15pm, on the 1st and 15th of every month:

    15 9 1,15 * * /path/to/script.sh

Conclusion

You have learned how to create cron jobs and schedule tasks at a specific date and time.

Feel free to leave a comment if you have any questions.