Cron is a time-based job scheduler in Unix-like operating systems. Cron helps to run commands or scripts at a given time and date. After setup cron it will execute periodically.

Install the cron package on Ubuntu:

apt-get install cron

Check cron status:

systemctl status cron

Config Cron:

nano /etc/crontab

Syntax:

* * * * * command

In the syntax first * stand for representing minutes [0-59]. Second * stands for representing hour[0-23]. Third * stand for representing day [0-31]. Fourth star stands for representing month[0-12]. Fifth * stand for representing day of the week[0-7].

Here are some more examples of how to use cron’s scheduling component:

  • * * * * * - Run the command every minute.
  • 15 * * * * - Run the command 15 minutes after every hour.
  • 0,10,20,30 * * * * - Run the command every 10 minutes.
  • */15 * * * * - Run the command every 15 minutes.
  • 0 4 * * * - Run the command every day at 4:00 AM.
  • 0 4 * * 2-4 - Run the command every Tuesday, Wednesday, and Thursday at 4:00 AM.
  • 20,40 */8 * 7-12 * - Run the command on the 20th and 40th minute of every 8th hour every day of the last 6 months of the year.

Examples:

Example1:If we want to schedule on first day of each month at 11 PM.Command

0 11 1 * * /path/script.sh

Example2: Set up and run php script every day at 11 AM.

0 11 * * * /path/script.php

Example3: Set up and call url every day at 11 AM.

0 11 * * * curl http://www.google.com

Following options are available in crontab:

crontab -l : List the all your cron jobs.

crontab -r : Delete the current cron jobs.