http://www.mandrakeuser.org/docs/admin/acron2.html

MandrakeUser.Org - Your Mandrake-Linux Knowledge Base!

 
 

* DocIndex - Administration

Scheduling II - cron

* Specifying Execution Time
* Writing Your Own Crontab
* Example: Monthly Compression Of 'sent_mail' Folder

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

 

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) is to be executed:

  1. The first field denotes the minute,
  2. the second the hour,
  3. the third the day of month,
  4. the fourth the month (by number or short name)
  5. the fifth the day of the week (by number or short name)

An * (asterisk) marks an unrestricted field. This tells 'crond', "never mind what minute / hour / day / month / weekday it is, do it". Have a look at the last line. There you see: The command is executed at the 42th minute of the 4th hour (am) of the first day of a month. It doesn't matter which month (* in the fourth field) or which weekday (* in the fifth field).

A line like this:

* * * * * command

would execute command every minute, regardless of which hour / day / month it is. This should also explain how the first * in '/etc/crontab' works: 'execute the command in the first minute of every hour, on every day, in every month'.

* * * * mon command

would execute command every minute, but only on Mondays.

What does this do?

44 14 * 11 wed command

Got it? It executes command on 2:44 pm every Wednesday in November.

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 to 4:42 am on the first day of every month, if that day happens to be a Sunday. However, if field three and field five both list restrictions, command is executed, when either field matches. So in this case on every first day of a month and on every Sunday.

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.

* section index * top

* 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.

Commands that take two or more lines are not allowed. Put them into a script and call the script from the crontab instead.

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=""
job1br /> MAILTO=jim
job2

For job1, no mail will be sent (even if the job fails), the mail concerning the second job will go to user 'jim'.

* section index * top

* 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:


#!/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_mailMonthYear.tar.gz' and then empty the 'sent_mail' folder/file.

* section index * top

* 'anacron', a flexible 'cron' add-on

 
Legal: All texts on this site are covered by the GNU Free Documentation License. Standard disclaimers of warranty apply. Copyright LSTB (Tom Berger) and Mandrakesoft 1999-2002.