This
article is the second in the Cron series. In this article I shall
show you some examples of various combinations that you can make
for entering commands in the crontab file. If you haven't gone
through the man pages for cron then you would most probably have
not thought of the various combinations that you will see in this
article.
In case you have no idea about cron then I suggest you read Article
No. 16 to get the basics right. Cron is an extremely useful
tool available in Linux. So if you haven't heard of cron before
please do read the article. In this article I shall be directly
mentioning the points without going into the details of cron.
You would be introduced to concepts such as Ranges, Step Values..
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.
|
|