cron is a program on UNIX-based systems that enable users to schedule run programs or scripts automatically at a specified time/date and are termed as cron jobs. Scheduling the tasks uses cron expressions.
cron runs as a daemon process i.e., once started it runs in the background. It makes use of crontab to read the entries of the scheduled tasks and triggers it.

<seconds> <minutes> <hours> <day-of-month> <month> <day-of-week> <year> <command>

<command>:represents the instruction file/program to run as a cron job

Some applications of cron jobs include backing up files, synchronizing files, scheduling weekly updates etc.

Cron Expressions

It is a string of six or seven sub-expressions separated by whitespace. It is widely adopted and apart from UNIX-based systems, we can see the use of cron expressions in programming languages and utilities.

Sub-expression position Denotes Required Allowed values Allowed special characters and symbols
1 Seconds Yes 0-59 , – * /
2 Minutes Yes 0-59 , – * /
3 Hours Yes 0-23 , – * /
4 Day of month Yes 1-31 , – * ? / L W C
5 Month Yes 0-11 or JAN-DEC , – * /
6 Day of week Yes 1-7 or SUN-SAT , – * ? / L C #
7 Year No 1970-2099 , – * /

Special characters, symbols, and their meaning:

  1. , (values): represents multiple values separated by commas
  2. * (all): represents all/every possible field values
  3. – (range): represents the range of values
  4. / (increment): increments by latter value

E.g: a/b: represents (a), (a+b), (a+b+b), (a+2b+b) … values within domain range of respective field values

  1. ? (any): represents arbitrary value, therefore its field value is neglected, and hence its effect
  2. L (last): last day of the month, last day of the week. Some variations to this are:
    • L-4 for: 4th to the last of the month
    • 5L for: represents last Thursday

     7. W (weekday): represents the nearest weekday to the day of the month. (Weekdays are Mon to Friday) E.g.,

       11W: if the 11th day of the month is Saturday, then the event will trigger on the 10th day of the month (Friday)

And if the 11th day of the month is Sunday, then the event will trigger on the 12th day of the month (Monday)

       LW: represents the last weekday of the month

  1. C (calendar): represents the first day on and after the nth day, values are calculated against the associated calendar E.g.,

      6C for represents the first day included by the calendar on or after 6th

      2C for represents the first day included by the calendar on or after Monday

  1. # (nth occurrence): represents the nth occurrence of weekday of the month

E.g., 5#2: represents the 2nd occurrence of Thursday of the month

Cron special strings:

There are some predefined annotation-like syntaxes that we can also use in cron expressions:

  • @reboot – run once at the startup
  • @yearly or @annualy – run once a year
  • @monthly – run once a month
  • @weekly – run once a week
  • @daily or @midnight – run once a day/in a midnight
  • @hourly – run hourly

Some use cases and their cron expressions:

  1. Clear logs using script, every Sunday at 5:30 am

0 30 5 ? * SAT /home/arpit/tasks/

  1. Send wishes to employees using script, on 25th December every year at midnight

0 0 0 25 DEC ? * /home/arpit/tasks/