Cron Job Linux

About Cron: The Linux Cron Job is a utility that can be used to execute or shedule a particular task
in the background at a specific time/date on an on-going basis. It is very helpful to get done the tasks automatically at the background and also time saving.

Linux Crontab Format

MIN
Minute field

0 to 59

HOUR
Hour Field

0 to 23

DOM
Day of Month

1 to 31

MON
Month Filed

1 to 12

DOW
Day of Week

0 to 6

CMD
Command

Command to be executed

Commands:

1. Scheduling a Job For a Specific Time
As already said, the basic intention behind the cron is to execute or run a specific job(task) in a specified time.

The cron given below will trigger the full back-up shell script(full-backup) on the specified time in the job.

30 07 08 09 * /home/sam/full-backup


30 – 30th Minute
07 – 07 AM
08 – 08th Day
09 – 9th Month (sept)

* – Every day of the week

2. Schedule a Job For Multiple Instance
We can schedule the cron to run at multiple instances, this will help to run the scripts at two different time specofied in the script. (for example twice a day).
The time instances are seperated using a comma operator in the script.
The following command will take the incremental backup (incremental-backup) shell script in the times specified.

00 11,16 * * * /home/sam/bin/incremental-backup


00 -0th Minute (Top of the hour)
11,16 – 11 AM and 4 PM

  • – Every day
  • – Every month

* – Every day of the week

3. Schedule a Job for Specific Range of Time
It helps to schedule a job for a specic range of time or in certain intervals.
An example to check the dabase status is shown below(every day) during the specified hours.

00 09-18 * * * /home/sam/bin/check-db-status


00 – 0th Minute (Top of the hour)
09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm

  • -Every day
  • -Every month

* -Every day of the week

The above cron job can be also set to check-db-status on every week days using the command shown below.
00 09-18 * * 1-5 /home/sam/bin/check-db-status

4. Command to view current cronjob entries for a user

The current logged-in user’s crontab entries can be viewed using the following command.

veeble@EcLinux:~$ crontab -l

no crontab for veeble

5. Command to view current cronjob entries for the root
The current root crontab entries can be viewed using the following command.


root@NixLinux:~$ crontab -l @monthly /home/veeble/monthly-backup 00 09-18 * * * /home/sathiya/check-db-status


6. Schedule a Job for Every Minute Using Cron

Ideally you may not have a requirement to schedule a job every minute. But understanding this example will will help you understand the other examples mentioned below in this article.

  • * * * * CMD

*/5 in minute field indicates every 5 minutes.
0-10/2 in minute field inidicates every 2 minutes in the first 10 minute.
So, we can use the above method for all the other 4 fields.

Schedule a Background Cron Job For Every 10 Minutes

*/10 * * * * /home/sam/check-disk-space

Instead of specifying values in the 5 fields, we can specify it using a single keyword as mentioned below.
There are special cases in which instead of the above 5 fields we can use @ followed by a keyword such as reboot, midnight, yearly, hourly.

Keyword Equivalent
@yearly 0 0 1 1 *
@daily 0 0 * * *
@hourly 0 * * * *
@reboot Run at start up

a) Schedule a Job For First Minute of Every Year using @yearly

@yearly /home/sam/red-hat/bin/annual-maintenance

b) Schedule a Cron Job Beginning of Every Month using @monthly

@monthly /home/sam/red-hat/bin/annual-maintenance

c) Schedule a Background Job Every Day using @daily

@daily /home/sam/red-hat/bin/annual-maintenance

d) How to Execute a Linux Command After Every Reboot using @reboot?

@reboot CMD

Enjoy freedom with Linux, its awesome 🙂

Leave a Reply

Your email address will not be published.