The Linux Cron - Crontab and cron jobs - Why, What, How?

| | 3 min read

In every operating system, there are a lot of tasks that need to be scheduled to run at a particular time - some, very often (as in, every 5 minutes), and some, less (like, once every month).

For a quick example, you might want to run a temporary files deletion operation at midnight each day - this is where you need to write a cron job.

Crontab and Cron jobs are Linux’s version of a complete job scheduling system.

Let us see these in detail.

Cron allows tasks to run automatically in the background at fixed time or time intervals depending upon the necessity of the task that has to be executed. We normally use cron jobs to automatically take backup from servers, or synchronise different folders or files and much more.

The task that is to be executed is known as a cronjob, and crontab can be defined as a table that stores the list of cronjobs that are being executed in a system. (The word “Cron”derived from “Chronos” which in greek means “Time” and “Tab” refers to “Table”. So crontab effectively means timetable).

Now let us see how to get playing with these for our uses.

Basic crontab file operations:

To know the list of cronjobs that is running on your system, just open a terminal and type in the following command:

crontab -l

In order to edit this crontab file, you can use the command

crontab -e

Removing a cron job:

Removing a cronjob is easy, just delete the cronjob line set in the crontab file and then save the changes in the crontab file.

Disabling a cron job:

To disable a cron job without actually removing it from the file, you can just comment it out by adding a '#' to the beginning of the line.

Adding a cron job:

In order to add a cronjob you have to understand the format of a cronjob line.

Cronjobs are written in the following format:

 * * * * /path/to script/filename.sh

Here the five stars represent various time/frequency parameters - minute, hour, day of month, month, day of week, respectively.

Minute – (0-59)
Hour – (0-23)
Day of month – (1-31)
Month – (1-12)
Day of week – (0-6) “Number 0 in week refers to sunday”

Using '*' to denote different frequencies:

'*' stands for 'every',
'*/2' stands for 'once in every two minutes', 
'*/3' stands for 'once in every three minutes'.

After these five stars you have to give the path where the script is located.

Examples:

The following examples should help you get a clearer understanding:

  * * * * /path/to/command/to/be/run

would mean you want to run the script every minute of every hour of every day of the week of every month of every year. This simply means that the script will get executed in every single minute.

  */2 22 8 12 * /path/to/command/to/be/run

would run the command once every 2 minutes, during the 22nd hour of the 8th day of the month, in the 12th month (December), whatever be the weekday ('every').

Hope this gave you an understanding of how to write a new cron job and how to play with the crontab file for your scheduling needs.

Let us know via the comments box below if you have any further doubts. We'll be happy to help you get kicking.

Happy cron-ing!!! :-)