Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scheduling tasks using Cron
#1

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.

 

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'.

 

 

Procedure :

 

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]'

 

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.

 

Few Tips On Cron :

 

To know what tasks cron shall be executing you can use the following command

$ crontab -l

This command shall provide a detailed list of all the jobs that cron shall execute. It would basically be showing you your crontab file. So you need to know the meaning of the 6 fields to make sense of the output

 

_

 

To remove the current crontab you can use the following command

$ crontab -r

This shall remove whatever entries you added to cron using your own crontab file.

 

_

 

To edit the crontab to enter or remove tasks type the following command

$ crontab -e

But note that typing this command would mostly open ' vi ' in order to edit the crontab file. And in case you are not familiar with vi then you would not like this behaviour. In that case open your crontab file (the one in which you entered all the tasks) using whichever text-editor you want, then add/remove the lines you want to, and then after saving the file run the following command

$ crontab [filename]

This will update the crontab with your new tasks. You can also check whether the update has taken place by typing the following command

$ crontab -l

 

_

 

If in a crontab file entry, the 'Day of Week' and 'Day of Month' fields are both restricted (i.e. Both are NOT * ) then the task would be executed when either condition gets satisfied.

E.g. Day of month is 1-10 and Day of week is 3, then the task would be executed on the the first 10 days of the month and thereafter on every Wednesday that occurs in the month.

 

_

 

To run a task every 5 hours

If you want to do the above then in the crontab file entry, in the hour field you can enter a Step Value as follows '*/5' (without the inverted commas). This would cause the task to be executed every 5 hours.

 

_

 

To run a task in the first 10 days of a month and then last 10 days of the month you can use Ranges as follows

Enter '1-10,21-30' (without the inverted commas) in the 'Days' field.

 

_

 

To run a task every alternate day for the first 15 days of the month you can enter '1-15/2' in the 'Days' field. This would run the task on the following days of the month (1,3,5,7,9,11,13,15)

 

_

 

If you want to enter Comments, all you have to do is to add a # at the beginning of the line. Remember comments are not allowed on the same line with a cron command. So comments need to be entered on a separate line starting with a # (first character should be #)

 

_

 

This tip is important for all those countries that use daylight saving techniques. If a particular instant of time (when a task was to be executed) is lost because of changing the time for daylight saving then cron will simply skip that task and wait for the next occurrence of that particular time. Similarly if a particular instant of time occurs twice because of change in timing, then cron would once again execute that task a second time.

 

_

 

Besides comments and cron commands you can also make environment setting in the crontab file. In case the program that you would be executing requires any environment variables to be set, than those too can be set in the file that cron would use. An environment setting can be simply made using a name=value pair.

E.g. typing HOME=/home/david/myprograms on a separate line in the crontab file would make that particular directory your HOME directory.

 

_

 

More cron tips in the future articles in this series. Till then hope you have a good time with the information presented here. Do mail me in case you have any problems with cron.

Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)