Thursday, March 25, 2010

Quick Understanding Linux Cron in under 3 Minutes

What is Cron?
In Layman Languages, cron is a Task scheduler used in Linux operating System (but with Great Powers if used with it Complete functionality) developed by Paul Vixie. You can schedule a specific task to be run every minute,hour,day,month.

Majorly cron is used to automate jobs daily without human intervention or manual running of command. Cron can be configured using configuration files which resides in "/etc", changes to which can be done only by the Administrator. Cron also has the feature to run commands using particular user.
Crontab entries


| --------------------------------------------------------------    Minutes (00 - 59)
|    ------------------------------------------------------    Hours (00 - 24)
|    |    ----------------------------------------------    Day of Month (01 - 30)
|    |    |    --------------------------------------    Month (01 - 12)
|    |    |    |    ------------------------------  Day of Week (0 - 6) (Sunday = 0)
|    |    |    |    |    ----------------------    User to the Run the Command
|    |    |    |    |    |        ------    Command / Script to be Executed
|    |    |    |    |    |        |

*    *    *    *    *    root    /home/linuxmaza.com/htdocs/scripts/Stats_Update.sh

If you are a lazy System Adminstrator and Dont want to write the whole entry, cron has several special entries which are shortcuts for specifying a Complete entry.
You can specify entries as below

Entry       ||    Description          ||    Equivalent To
=====================================================
@reboot     Run once, at startup.     None
@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 * * * *

Operators in Cron
There are way of specifying multiple date and time in a field which can be overcomed using Operators in cron.

  • The comma (',') operator specifies a list of values, for example: "1,3,4,7,8" (Spaces between the values are not accepted)

  • The dash ('-') operator specifies a range of values, for example: "1-6", which is equivalent to "1,2,3,4,5,6"

  • The asterisk ('*') operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to 'every hour' (depending on other values entered in other fields).

  • The Forward Slash ('/') operator is used to SKIP a given number of values. "Say the value we used is 5" "For Example, "*/5" in the hour time field is equivalent to "0,5,10,15,20" and so on.


Cron Advantages :
1. Can Handle Multiuser
2. Cron enables users to schedule jobs to run automatically at a certain time or date.
3. Automate daily System Administration tasks
4. Operators in Cron saves time and confusion while creating many crons.
5. Support for Special entries using (@reboot, @yearly, @monthly, @weekly, @hourly, @midnight, @daily).


Some Examples
1. If you want to run a task every Hour

0 * * * * root w > /var/log/mysystemstats.log
OR
@hourly root w > /var/log/mysystemstats.log

Above entry would run the "w" command at the start of every hour and redirect the output to "/var/log/mysystemstats.log"

2. Scheduling a cron job running every 15 minutes to delete apache Logs

15 * * * * echo " " > /var/log/httpd/access_log

3. Run a job every 5 hours
* 0,5,10,15,20 * * * echo " " > /var/log/httpd/access_log

No comments:

Post a Comment