|[Home] [Credit Search] [Category Browser] [Staff Roll Call]||The LINUX.COM Article Archive|
|Originally Published: Thursday, 16 September 1999||Author: Paul Summers|
|Published to: news_learn_firststep/Firststep News||Page: 1/1 - [Printable]|
What do you want to execute today?
Perhaps one of the most useful Unix tools around; Cron is essentially a system Daemon, which executes scheduled commands. Learn the myriad of things you can do with this indispensable peice of software.
|Page 1 of 1|
Perhaps one of the most useful Unix tools around; Cron is essentially a system Daemon, which executes scheduled commands. These commands are contained in a Cron table, or "crontab". When running, the Cron daemon examines all stored crontabs, checking each command to see if it should be run in that given minute. As default, Cron will email the result of the scheduled event to the user whom set it.
Any user on a Unix system, by default, can create their own Cron table and Execute any system (or other) program of which they have permissions to do so. What use is this? Well, as much fun as it can be to sit in front of a computer 24 hours a day, 7 days a week to perform maintenance; it is much easier to just schedule those tasks for a given time. For example, one can easily set a crontab to use tar to perform a system backup at four in the morning, when it is unlikely that many users would be logged into a system. Another excellent administration tool is formed when one combine's perl with Cron to automate virtually any task imaginable.
Individual crontabs can usually be stored in any file you chose. If you wish, you can simply execute the command 'crontab -e' on most systems to use a standard file. If, however, you want to create an easy to remember file for your crontab, you may wish to create a file "cronjobs" in your home directory.
Each line in your crontab can be either an environment setting or a Cron entry. Lines which begin with the pound character ("#") are ignored as comments. Environment variables must be set in the following format:
VARIABLE = VALUE
Usually, the Cron daemon will set up normal user environment variables automatically, such as HOME, SHELL, USER, and so on. Cron will also look for a MAILTO variable in order to determine where it's output should be sent. You can leave this variable undefined, and the default will be used. Or, you can specify another email address, as well as a variation should you require it. So, lets say you have set up procmail to catch your Cron output mail with the address email@example.com you can then set the MAILTO variable to direct output there.
MAILTO = "firstname.lastname@example.org"
The Cron table includes five time variables followed by the command you wish to execute. Commands are run when the specified time variables match the system time. These time variable fields are as follows:
Minute 0-59 Hour 0-23 Day (Month) 0-31 Month 1-12 Day (Week) 0-7 (0 is Monday)
Any time variable with an asterisk ("*") will act as a wildcard, accepting all for the variable field. Number ranges can also be used, for example: "5-6" or "12-20" as well as lists of numbers ("2,3,4") or a combination of both ("2,3,4,6-12"). Adding even more functionality, one can use step values in a sequence. ("2-25/3").
With all that rather confusing information in mind, the actual Cron table is quite simple. Say we wanted to use perl to execute the script /usr/local/bin/myscript
30 4 * * * perl /usr/local/bin/myscript
This would execute the perl script every day at 04:30am. Now, lets say we only want to execute our script on the first day of every month, but at the same time. The following would do exactly that.
30 4 1 * * perl /usr/local/bin/myscript
Now, getting into a little more advanced example, say we wanted to perform an 'ls' of a directory, and dump the output to a file.
0-30/10 12,18 * * 1-4 ls -l /home/user/ > /home/user/output.txt
This would dump the output of 'ls -l /home/user/' to the file output.txt in the user's home directory, on monday to thursday, every ten minutes for the first 30 minutes of the hours of 12:00 pm and 6:00 pm.
So, now that you have your crontab table, you will need to install it. If you used the 'crontab -e' command, your crontab will be saved automatically when you exit the editor you used to create it. If your crontab file is a file you named yourself, you will need to execute the following command:
Assuming that 'cronjobs' is your crontab file. You can also list the current Cron actions which you have scheduled by using 'crontab -l', and you can remove them all with 'crontab -r'.
By: Paul "Simba" Summers.
|Page 1 of 1|