Left Menu




Howto use Cron for first time


What is Cron?


Cron is a time-based job scheduler in Unix-like computer operating systems. 'cron' is short for Chronograph. Cron enables users to schedule jobs (commands or shell scripts) to run automatically at a certain time or date. It is commonly used to perform system maintenance or administration, though its general purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.
 
What Cron Does?

Cron searches /var/spool/cron for crontab files which are named after accounts in /etc/passwd; crontabs found are loaded into memory. Cron also searches for /etc/crontab and the files in the /etc/cron.d/ directory, which are in a different format. Cron then wakes up every minute, examining all stored crontabs, checking each command to see if it should be run in the current minute.
How can cron be used?


The cron syntax is really simple, but may be a little difficult for first time users :

You can edit the file by executing "crontab -e"

1 2 3 4 5 command

1 - Minute (0-59)
2 - Hour (0-23)
3 - Day of the Month (1-31)
4 - month (1 - 12) OR jan,feb,mar,apr ...
5 - day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
command -  cp  perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -config=www.linnewbies.com -update

When you want it to be repeated on every minute,hour,day,month you can just put *

Example :

* * * * * perl /usr/local/awstats/awstats.pl -config=www.linnewbies.com -update
 
This will execute the perl script /usr/local/awstats/awstats.pl and update the awstats DB

*/2 * * * * perl /usr/local/awstats/awstats.pl -config=www.linnewbies.com -output -staticlinks > /home/kickar/stats.html

This will execute the perl script /usr/local/awstats/awstats.pl and reproduce the file /home/kickar/stats.html every two minutes, so we can check the stats of the website :)

There are also some shortcut entries :

@reboot - run once, at start up
@yearly - Run once a year = 0 0 1 1 *
@annually - (same as @yearly) = 0 0 1 1 *
@monthly - Run once a month = 0 0 1 * *
@weekly - Run once a week = 0 0 * * 0
@daily- Run once a day = 0 0 * * *
@midnight - (same as @daily) = 0 0 * * *
@hourly - Run once an hour = 0 * * * *

To execute multiple commands in the same time you can use "&&". For example:

* * * * * mkdir /home/cron && mv /home/cron /home/cron1

This will make the directory /home/cron and after that rename it to /home/cron1

If you want you can specify already existing crontab file, you can set it to be used with the following command:

crontab -u "username" "crontab file"

For example :

crontab -u kickar ~/crontab

.. would set kickar's crontab file to that of the file named "crontab" residing in kickar's home directory.

If you execute the command "crontab -l", you will see the current crontab file for the user ou executed the command as:

for example kickar's output will be :

kickar@linnewbies $ crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.XXXXUx0if5 installed on Sat Nov  7 19:24:27 2009)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
* * * * * perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -config=www.linnewbies.com -update
* * * * * perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -config=www.linnewbies.com -output -staticlinks > /home/kickar/stats.html

kickar@linnewbies $

To remove a scheduled job just execute "crontab -r" as the user you need to remove the task!


For more info execute "man cron" or "man crontab"
If you have questions refer to our Forum



Delicious de.li.cio.us


|Home | LinuXchange| Updates | Contact Us | About The Project | Unique Visitors| Sponsorship|