Scheduling II - cron
Summary:
- Specifying Execution Time
- Writing Your Own Crontab
- Example: Monthly Compression Of 'sent_mail' Folder
Strictly speaking, there's no program called 'cron'. There is the 'cron daemon', crond, which is started automatically at boot, and the command 'crontab' to set up files to control 'crond', the 'crontabs'. Both 'Linuxconf' and 'Webmin' offer modules to configure 'cron' via a graphical interface.
The crontab system in Mandrake Linux is twofold: system wide crontabs are controlled by '/etc/crontab' and '/usr/bin/run-parts', which call scripts stored in '/etc/cron. d' (executed every minute), '/etc/cron.hourly', '/etc/cron.weekly' and '/etc/ cron.monthly'.
Crontabs for users (and 'root') however are stored in '/var/spool/cron/user_name'.
Specifying Execution Time
Have a look at '/etc/crontab', because it will tell you something about the 'crond' syntax. By default, the lower part of this file looks like this:
01 * * * * root run-parts /etc/cron.hourly 02 4 * * * root run-parts /etc/cron.daily 22 4 * * 0 root run-parts /etc/cron.weekly 42 4 1 * * root run-parts /etc/cron.monthly
Each line starts with five fields denoting when the command (
runparts directory
- The first field denotes the minute, * the second the hour, * the third the day of month, * the fourth the month (by number or short name) * the fifth the day of the week (by number or short name)
*
*
*
A line like this:
* * * * * command
would execute
command
*
* * * * mon command
would execute
command
What does this do?
44 14 * 11 wed command
Got it? It executes
command
Note that field three (day of month) and field five (day of week) are handled a bit differently, if they both list restrictions.
Assume you would change the last line of '/etc/crontab' to
42 4 1 * sun command
One might be inclined to think that this would restrict the execution of
command
command
You can also specify (multiple) ranges (x-y), lists (x,y,z) and step values (in conjunction with ranges, x-y/z):
05-15,30-40/3 8,12,16 * */2 7 command
Tough one, eh? ;-) Here's a hint: When you are trying to decipher rather complicated 'crond' rules, read the fields from right to left:
The command is executed on Sundays (7), every second month (*/2, i.e. January, March, May etc), during the hours 8, 12 and 4pm. It is executed every minute between the fifth and fifteenth minute and every three minutes between the thirtieth and fortieth minute.
So in January 2003 the command would be executed at the 5th, 12th, 19th and 26th of January (Sunday), at every minute between 8:05am and 8:15am, and then at 8:30, 8:33, 8:36 and 8:39. Then at 12:05, 12:06, 12:07 etc.
Writing Your Own Crontab
To edit / create your crontab, issue
$ crontab -e
which opens a new file (or your existing crontab) in an editor.
Note that 'crontab' uses the (unjustly) infamous 'vi' editor by default. To make 'crontab' use your favorite editor, e.g. 'gedit', run this command before calling 'crontab':
$ export EDITOR=/usr/bin/gedit
There are also several graphical crontab editors available. KDE users may prefer KCron (included in the 'kdeadmin' package, menu: Configuration - Other - Task Scheduler) to the orphaned 'kcrontab', others Tct to the license-encumbered 'VCron' or the unstable 'GCrontab'.
The other options to 'crontab' are:
- crontab -l, which lists your crontab and * crontab -r, which deletes it.
If you want to execute commands which need the permissions of another user, e.g. 'root', use su to login into that account and run 'crontab' like this:
~# crontab -e -u root
If the commands run via 'crond' produce any output or if errors occur, a mail is sent to the owner of the crontab the command was started from, provided a mail daemon like 'Postfix' is installed and running. This might be inconvenient for 'root' cron jobs, if you are your own administrator and don't get 'root's' mail. This behavior can be changed with the MAILTO variable in your crontab. It defines who should get mail for which job(s), or that no one should get mail from cron at all:
MAILTO=""<br> job1br /> MAILTO=jim<br> job2
For
job1
Example: Monthly Compression Of 'sent_mail' Folder
You want 'cron' to compress your 'sent_mail' file or folder, store it somewhere else and then empty the file or folder. Create a script like this:
~#1.1 /bin/bash ~# tar czf $HOME/mail/sent_mail$(date +%b%Y).tar.gz /path_to/sent_mail_folder_or_file ~# for mail file (mbox format) use this: if $?=0; then echo > /path_to/sent_mail_file else exit 1 fi ~# for mail folder (mh format) use this: if $?=0; then rm -f /path_to/sent_mail_folder/* else exit 1 fi
(The 'if' construction prevents the cleaning of the sent_mail folder/file if 'tar' has failed to create an archive.)
Make the script executable with chmod +x file. Run a test with a backup file - it's always a good idea to test your scripts before they can do damage ;-).
Next edit your crontab with crontab -e and put these lines in it:
# the next line catches all months with 31 days 59 23 31 1-7/2,8-12/2 * sh your_script_with_full_path # the next line all months with 30 days 59 23 30 4,6,9,11 * sh your_script_with_full_path # well, and February. Will be one day off in leap years 59 23 28 2 * sh your_script_with_full_path
If everything goes well, 'cron' will call the script on the last minute of each month. The script will compress the 'sent_mail' folder/file, store it under '$HOME/mail/sent_mail~MonthYear.tar.gz' and then empty the 'sent_mail' folder/file.
Next Item: 'anacron', a flexible 'cron' add-on
Related Resources:
man cron
man 1 crontab
man 5 crontab
M dkRef I.5.3
Revision / Modified: Dec. 25, 2001 / Jan. 28, 2002
Author: Tom Berger
Legal: This page is covered by the GNU Free Documentation License. Standard disclaimers of warranty apply. Copyright LSTB and Mandrakesoft.
Version 1.5 last modified by imr on 21/01/2006 at 08:37
Document data
- Lost account?
- Join the community, be part of the Club: it's free!
- Get the PWP Download Subscription!