Using cron jobs to schedule repetitive tasks on Linux

What is cron:

The software utility cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like downloading files from the Internet and downloading email at regular intervals.[CRON]

Cron is most suitable for scheduling repetitive tasks.

Using cron on linux

Open cron

crontab -e

Result

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

Create our cron

# add the cron execution to the end
m h dom mon dow command

Cron is created using the form m h dom mon dow command

  • m: the minutes
  • h: the hours
  • dom: day of month
  • m: the month
  • dow: day of week
  • command: command to run

Crontab scheduling tips:

  • * * * * *: run every minutes
  • 15 * * * *: run on minute 15 every hour (00:15, 02:15, ..., 23:15)
  • 15 23 * * *: run on 23:15 every day (25/01/2019 23:15, ..., 28/02/2020 23:15, ...)
  • 15 23 03 * *: run on 23:15 every third day of each month (03/01/2019 23:15, ..., 03/02/2020 23:15, ...)
  • 15 23 03 9 *: run on 23:15 every third day of september every year (03/09/2019 23:15, ..., 03/09/2020 23:15, ...)
  • 15,16,17 * * * *: run on minutes 15, 16, 17 every hour (00:15, 00:16, 00:17, 02:15, 02:16, ..., 23:15)
  • 15-17 * * * *: run on minute 15, 16, 17 every hour (00:15, 00:16, 00:17, 02:15, 02:16, ..., 23:17)
  • 15-19/2 * * * *: run on minute 15, 17, 19 every hour (00:15, 00:17, 00:19, 02:15, 02:17, ..., 23:19)
  • 00 12 * * Thu: run every Thursday at 12 p.m
  • 00 12 * * 0: run every Sunday at 12 p.m

Syntaxe:

  • *: all valid values. Example: * 12 * * * equles: run on every minutes when hour is 12 (12:00, 12:01, ..., 12:59).
  • x,y,z: enumerates multiple values: runs on every item selected.
  • a-b: a range of values: runs on every element between a and b. Example: 54-57 equals: 54, 55, 56, 57.
  • a-b/n: runs on every element between a and b by incremanting n. Example 1: 2-15/3 equals: 2-5-8-11-14. Example 2: */5 * * * * means run on every five minutes.
  • * * * * n: n (day of the week) is between 0 and 6 where 0 is Sunday or the abbreviation of the day like Thu(Thursday).