Scheduling II - cron
Summary:- Specifying Execution Time
- Writing Your Own Crontab
- Example: Monthly Compression Of 'sent_mail' Folder
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
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)
*
*
*
* * * * * command
command
*
* * * * mon command
command
44 14 * 11 wed command
command
Assume you would change the last line of '/etc/crontab' to
42 4 1 * sun command
command
command
05-15,30-40/3 8,12,16 * */2 7 command
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. section index
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.
MAILTO=""<br> job1br /> MAILTO=jim<br> job2job1
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
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
Related Resources:
man cronman 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.
Scheduling II - cron
Version 1.5 last modified by imr on 21/01/2006 at 08:37
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!
Mandriva.com
Store
Club


