Library Technology - Reviews, Tips, Giveaways, Freeware

Library Technology – Reviews, Tips, Giveaways, Freeware

Scheduling tasks using Cron – Part I

Posted In Linux - By Techtiplib on Saturday, June 16th, 2012 With No Comments »

This article explains task scheduling using a program called cron which is used for complex task scheduling. Scheduling of tasks under Linux is an extremely powerful procedure which is used by almost everyone. The basic advantage of cron over at is that if you want a task to occur at regular intervals or basically more than once, then you don’t have to repeatedly enter ‘at‘ commands multiple times. You can use cron and feed in the number of times that you want the task to occur and cron handles the details of executing that task again and again.

In case you just want one particular task to execute at one particular time then you can get away with simpler task scheduling procedures that are available such as ‘at‘. This is explained in “Execute a task ‘at’ the time you want on this website.

Cron basically uses a particular table which has all the information about the task you want to execute and when you want to execute it. The user has to make this table and ask cron to use that table for its scheduling. You can make this table using any text editor such as vi or emacs and store it in any text file. This file is called the crontab file and I shall name mine as ‘myjobs’.


Simply make a new text file, and enter this line in that text file. I have named my text file (crontab file) as ‘myjobs’

30 04 * 3-5 * backup

Save the file and then run this command at the prompt

$ crontab myjobs

Thats it !! Now you will have your program called backup (in case you really have one) running at the times you have set it to run.To run whichever program you want, you have to make similar entries in the crontab file that you make and simply tell cron which file you have made using the command ‘crontab [filename]’

Format of the Crontab File: 

The crontab file consists of lines each explaining a task. Each line consist of 6 fields. I shall explain the 6 fields with the help of the example that I have used in the above discussion.

Allowed range
My Example
Minutes that have to pass after the selected hour in order to execute the task
30, which means 30 minutes after the selected hour
Hours at which the task has to be executed
04, which means at 4 O’clock in the morning
Days of the month on which this task has to be executed
*, which means that every day of the selected month
Months during which the task has to be executed
1-12 OR First 3 letters of the Month name. Case doesn’t matter. E.g. Jan
3-5, which means run the task in the months of March, April & May
Days of the week on which this task has to be run
0-7 OR First 3 letters of the Day name. Case doesn’t matter. E.g. Sun
(0 or 7 is Sunday, 1 is Monday…)
*,which means all days of the selected weeks
Name of the program (task) to be executed
Any program
backup, which is a
program that I have which makes a backup of all the important files that I have on my machine

Basically the example I used above in the crontab file was the one I had in my file when I was working on an important project in the month of March. The project was going to take approximately 3 months and I used to stop working at around 4 in the morning everyday. So 4.30 in the morning was a perfect time to take a backup of all my important files. This backup had to be taken everyday for the 3 months. The program named backup did the workof actually making a backup of my files. Cron simply executed that program everyday and Cron did it’s work perfectly well for me.

Remember that all the output that your executing program generates, will be sent to you as a local mail. Not your ISP mail but your local Linux mail. So next time you check your local mail, you would find a mail whose contents would be the output of your task that cron executed. I mean suppose you used cron to execute the ‘ls‘ commmand at regular intervals, then you would receive mails whose content would be the directory listing of your linux filesystem.


More contents in: , ,

About - Hey, this blog belongs to me! I am the founder of TechTipLib and managing editor right now. And I love to hear what do you think about this article, leave comment below! Thank you so much...