Schedule tasks using CRON

In a computer program, there would be instances where you would want to automatically run a script periodically. To do this, we need to use a linux utility called cron.

Cron is a Linux software utility which enables users to automatically run commands or scripts at a scheduled time. A crontab is a file which contains a list of commands  or instructions to be ran at specified times. Each line of a crontab file represents a job, and is composed of a CRON expression, followed by a shell command to execute.

CRON Expression

A cron expression is a string made up of five or six fields separated by white space that represents a set of time, normally as a schedule to execute some routine.

Syntax:

 # ┌───────────── min (0 - 59)
 # │ ┌────────────── hour (0 - 23)
 # │ │ ┌─────────────── day of month (1 - 31)
 # │ │ │ ┌──────────────── month (1 - 12)
 # │ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
 # │ │ │ │ │
 # │ │ │ │ │
 # * * * * *  command to execute

Cron Job

A Cron Job is a scheduled task which runs on server according to the set time/date. It is very useful for running repetitive tasks like running backup script, sending out email notifications or a batch script.

E.g.
0 6 * * *  /usr/bin/php  /home/someone/script/email.php

This example runs a PHP script at 6:00 AM every day. It is made up of a CRON expression followed by the path for php and the path of the script to execute.

 

Resource(s): https://en.wikipedia.org/wiki/Cron#Modern_versions