20 Linux Crontab Command Pratical Usages in day to day work

By | January 17, 2017

Crontab is a command is used to execute the list of commands at scheduled time.It is file to store the scheduled commands too.Crontab is used by most of the time by System Administrators to schedule the regular server Activities like Backup and Running scheduled jobs.

Crontab is daemon process which runs in the background always to check the crontab schedule.I believe in giving enough Examples.So,let’s go by few examples before we jump on to the actual Crontab syntax’s.

 20 linux crontab examples


20 linux crontab examples

Example 1:

we can run Specific Program at Early morning 5 AM in day.

Example 2:

We can run Specific Program at Night 11:59 PM.

Example 3:

Some times,we have to run multiple programs/processes at a time at exact second.It is difficult to do the above activities without without Crontab.Crontab can be maintained at each user level.(i.e) Every user can have it’s own individual Crontab File or Setting.

All the user Crontab files will be stored in “/var/spool” path.

Usage 1: How to See the Existing Crontab for the logged in user.?

Crontab -l

It displays the content as below.

[[email protected]]# crontab -l
no crontab for root

It seems,we don’t have crontab contents for the user – user1 on machine name called “LinuxMachine”.

Usage 2: How to add the contents to crontab ?

[[email protected]]# crontab -e

Usage 3: How to remove the contents from crontab?

[[email protected]]# crontab -r

Usage 4:How to remove the contents from crontab with yes or no prompt.?

[[email protected]]# crontab -i

Usage 5:In Crontab,each Line is one Scheduled task.Each line should have six Fields.

  1. MIN - 0-59 in Value
  2. HOUR - 0 to 23 in Value ( Remember it is always in 24 hour format)
  3. DOM => Day of the Month - 1 to 31 in Value
  4. MON => Which Month it is. - 1 to 12 in Value
  5. DOW => Day of the Week.- 0 to 6 in Value
  6. CMD => Which command/Process you want to schedule with path of the executable.
   MIN HOUR DOM MON DOW CMD

USAGE 6:How to run Backup Job in the morning At 20th December at 5:15 AM using Crontab.?

15 05 20 12 * /home/user1/backupJob.sh

15 -> 15 Minutes

05-> 05th Hour.

20-> 20th Day.

12-> 12th Month

The above Entry in the Crontab will run the script – backupJob.sh at 5:15 AM on 20th December.

USAGE 7:How to run DailyJob Script in the morning Daily at 5 AM?

0 5 * * * /home/user1/MyDailyScript.sh

0 -> Oth Minute.

5 -> 5th Hour

* ->Every Day

* -> Every Month

USAGE 8:How to run the script  every 12am and 12pm on the 1st day of every 2nd month?(i,e Alternate month) ?

0 0,12 1 */2 * /home/user1/MyScript.sh

0,12 -> Means 12th hour and 24th hour.

*/2 -> Every Alternate month

USAGE 9:How to run the script every 3am on the 1st through the 10th of each month ?

0 3 1-10 * * /home/user1/MyScript.sh

1-10 -> means from 1st to 10th Date.

USAGE 10:How to run the script every month at 4am on Mondays, and on the days between 15-21.

0 5 15-21 * 1 /home/user1/MyScript.sh

1-> means monday

15-21 means days

USAGE 11:How to run the script for every hour between 1 am to 8 am that to daily.

0 01-08 * * * /home/user1/MyScript.h

USAGE 12:How to run the script for every Minute,daily.

0 * * * * /home/user1/MyScript.h

USAGE 13:How to run the script for every Second,daily.

Note: We can not schedule.Because minimum time we have to mention is 1 minute.

USAGE 13:How to run the script for every 30 Minutes.

0/30 * * * * /home/user1/MyScript.h

USAGE 14:How to run the script yearly once at first minute of the year.

@yearly /home/user1/MyScript.h

USAGE 15:How to run the script monthly once at first minute of the month.

@monthly /home/user1/MyScript.h

USAGE 16:How to run the script Daily once at first minute of the day.

@daily /home/user1/MyScript.h

USAGE 17:How to run the script After system Reboot.

@reboot /home/user1/MyScript.h

USAGE 18:How to Install the crontab from text file

Crontab crontab.txt

Note:Suppose ,if you have backup of crontab contents in the crontab.txt.We can install directly from the previous backup using above command.

USAGE 19:How to run the script At January 2 at 6:15 A.M.

15 06 02 Jan * /home/user1/backup.sh

USAGE 20:How to run the script At 10:30 P.M., every weekday.

30 22 * * Mon,Tue,Wed,Thu,Fri /usr/local/bin/backup

The following two tabs change content below.

SRINIVAS DARIPELLI

Myself SRINIVAS DARIPELLI. I have 15+ Years of Experience in Programming worked on multiple technologies.Apart from it,I am a blogger, writer, editor, artist and dad 🙂 .I believe in reality.I love to share the Helpful things around the Technology. Feel free to connect with me

Leave a Reply