



(10 ratings)
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]'
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.
|
Field
|
Meaning
|
Allowed
range
|
My
Example
|
|
1
|
Minutes
that have to pass after the selected hour in order to
execute the task
|
0-59
|
30,
which means 30 minutes after the selected hour
|
|
2
|
Hours
at which the task has to be executed
|
0-23
|
04, which means at 4 O'clock in the morning
|
|
3
|
Days
of the month on which this task has to be executed
|
1-31
|
*,
which means that every day of the selected month
|
|
4
|
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
|
|
5
|
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
|
|
6
|
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.
20 Random Tutorials from the same category :
Network programming under Unix systems
About your files on Linux
Parallel Programming - Basic Theory For The Unwary
Building And Using Static And Shared "C" Libraries
How to change a file's owner and group in Linux
Accessing CDROMs in Linux (covers mount, umount, fstab configuration file)
Setup of Ensonic Sound Card (PCI Sound card)
Manipulating directories in Linux
How to view text files in Linux
How to use the most popular command in Unix - Grep
Redirecting standard input and output
The great features of Linux - an alternative view
Linux XP - a joke becoming reality?
Windows COM Ports equivalent in Linux
Pipes - Get the most out of your shell
Introduction To Unix Signals Programming
Asking questions in a discussion forum
Scheduling tasks using Cron - Part II
How to find files in Linux using 'find'
Accessing User Information On A Unix System













