Cron

From Leo's Notes
Last edited on 5 October 2022, at 04:53.

Cron is a program that runs scheduled tasks on Unix based systems.

On newer systems running systemd, you might also be interested in systemd timers with has the same functionality as cronjobs.

Introduction

On Linux, the Cron service is available by the Cronie cron daemon project which is an extension of the original Vixie cron codebase.

Installation

On most modern Linux distributions, cron is available out of the box but can be installed by installing the cronie package.

On CentOS, Fedora, and Red Hat Enterprise Linux, run: yum -y install cronie.

Basic Usage

There are two types of cronjobs: System and per-user jobs.

  1. System jobs are defined in /etc/crontab, /etc/cron.d/ and can only be edited by root. These jobs can be made to run as any user on the system as the second argument in the cronjob requires a username the job is to run as. Side note: You can have scripts that run on a periodic schedule by placing them under /etc/cron.daily/, /etc/cron.weekly/, and /etc/cron.monthly/ directories. These are handled by Anacrontab which are triggered via the Anacron's system user cronjobs.
  2. Per-user jobs are defined in a separate file for each user in /var/spool/cron/ and can be edited by a user by invoking crontab -e. Jobs can be listed by running crontab -l.

The basic usage is to create a crontab file containing the following fields:

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |  ,- user to run as (only for system jobs in /etc/crontab)
# *  *  *  *  * user-name command to be executed

The username is required in /etc/crontab only. It must be omitted otherwise.

About the 'Day of Week' value

If you set the 'Day of Week' value to something other than '*', the cronjob will run on either the specified day of month or the specified day of week.

For instance, this cronjob will run at 2:15AM between the 1st and 7th of every month AND at 2:15AM every Wednesdays. It does not run on the first Wednesdays of each month.

15 2 1-7 * 3 root run-every-wednesdays-and-1-7-every-month

If you wish to run a cronjob on the first Wednesday of each month, you need to add a check with test $(date %u) -eq 3:

15 2 1-7 * 3 root test $(date +\%u) -eq 3 && run-on-first-wednesdays-every-month


Example time definitions

Min

0-59

Hour

0-23

Day of month

1-31

Month

1-12

Day of week

0-6

Description
*/5 * * * * Every 5 minutes
12 */3 * * * Every 3 hours at 12 minutes
57 11 15 1,6,12 * At 11:57 Hrs on 15th of Jan, June & Dec
25 6 * * 1-5 At 6:25 AM every weekday (Mon-Fri)
0 0 4,12,26 * * At midnight on 4th, 12th and 26th of every month
5,10 9,14 10 * 0,4 At 9:05AM, 9:10AM, 2:05PM and 2:10PM every Sunday and Thursday

Common intervals

Instead of defining the specific minute, hour, and date for a recurring task, you can use shorthand keywords:

Shortcut Equivalent Description
@yearly 0 0 1 1 * Every year
@annually 0 0 1 1 * Every year
@monthly 0 0 1 * * Every month
@weekly 0 0 * * 0 Every week
@daily 0 0 * * * Every day
@midnight 0 0 * * * Every day
@hourly 0 * * * * Every hour

Controlling user access to cron

The /etc/cron.allow file should contain a list of users that has access to crontab. By default, the file does not exist and denotes that any user has access to crontab.

You can deny all users access to crontab by touching/creating an empty file.

See Also