Search This Blog

Monday, May 9, 2011

CRON on Linux


Cron is a Unix utility that allows tasks to be automatically run in the background at regular intervals by the Cron daemon. These tasks are often termed as Cron Jobs in Unix. You will use it on your Unix or Linux operating systems for doing some tasks at specific intervals with out your intervention every time. You set the clock and forget. The Cron daemon runs the work for you.

What is crontab?


Crontab is a text file that contains a series of cron functions. Linux server uses a Cron Daemon to handle the Cron Jobs which are set in the Crontabs of different users. The Cron Daemon which handles this on Linux servers is called “Crond

Is my Cron running? If not, how to start the Cron?


 Cron is a daemon, which means that it only needs to be started once, and will lay dormant 
until it is required [Similar to a Web server daemon which stays dormant until it gets asked for a
web page]. The Cron daemon, or Crond, stays dormant until a time specified in one of the config
files, or crontabs.

On most Linux distros crond is automatically installed and entered into the start up scripts. 
To find out if it's running do the following:
arjun@vigilance: $ ps aux | grep crond
root       311      0.0  0.7  1284   112  ?          S    Apr24   0:00 crond
arjun    25645  0.0  0.0   4012   756 pts/3    S+   01:45   0:00 grep crond

Note: Its ps aux | grep cron for distros like Ubuntu.

The top line in the output shows that crond is running, the bottom line is the search we just run.

If it's not running then either you killed it since the last time you rebooted, or it wasn't started. 
The crond service can be stopped or started using the scripts below:
arjun@vigilance: $ /etc/init.d/crond stop
arjun@vigilance: $ /etc/init.d/crond start

Note: Use sudo if you are not a root user. For few distros, its /etc/init.d/cron start


Crontab Commands

$ crontab -e => Edit your crontab file, or create one if it doesn’t already exist.
$ crontab -l => Display your crontab file.
$ crontab -r => Remove your crontab file.
$ crontab -v => Display the last time you edited your crontab file. (This option is only available on a few systems.)
$ export EDITOR=vim.tiny => to specify a editor to open crontab file.

Crontab file


The general syntax of a Unix Crontab is :


[minute] [hour] [day of month] [month] [day of week] [command to run]


A Crontab file has six fields. The acceptable values for each of the 6 fields are:



Field
Range of values
minute
0-59
hour
0-23
Day of month
1-31
month
1-12
Day of week
0-7 (where both 0 and 7 mean Sun, 1 = Mon, 2 = Tue, etc)
Command to run
The command to run along with the parameters to that command if any

The fields have to be in that exact order, with no empty or missing fields. An asterisk (*) is used to indicate that every instance (i.e. every hour, every weekday, etc.) of the particular time period will be used.


Examples on how to set up Cron Job


Open the Crontab by:

arjun@vigilance:$ crontab -e


Now, press i [For switching into insert mode] and add your Cron Job like below examples. After adding, press Shift+Z+Z to save and exit from Crontab OR press esc and then type :q! And then hit enter to quit without saving.
 

Examples


# Run Webalizer to update Apache Log file at the 28th min of every hour.

28 * * * * /usr/bin/webalizer


# Run Webalizer to update Apache Log files every 10 minutes.

*/10 * * * * /usr/bin/webalizer



# Run /usr/bin/backup on the 15th of any month at only if its a Saturday

0 12 15 * 6 /usr/bin/backup



# A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.

30 18 * * * rm /home/someuser/tmp/*



# At 12.05,12.10 every Monday & on 10th of every month

5,10 0 10 * 1 your_command



# If you want the cron job to run at 1:00 and 2:00 A.M every day, every month and every week.

* 1,2 * * * /home/username/www/members.php




# If you want to run the above task only from Monday to Friday and at 11.30 P.M

30 23 * * 1-5 /home/username/www/members.php



# Will run connection.test every 15 mins between the hours or 9am and 5pm
*/15 9-17 * * * connection.test


# This command is run at 4 am every Sunday
* 4 * * Sun cat test.txt << “Gotcha!!”


# This command is run 4:42 am every 1st of the month
42 4 1 * * cat test.txt << “Gotcha!!”



# Will run cmd every midday between the 1st and the 15th as well as the 20th 
and 25th (inclusive) and also on the 17th of every month.

* 12 1-15,17,20-25 * * cmd


Some of the options which can come handy are given below


Minutes(s)(0-59)
Value

Hour(s)(1-23)
Value
Every Minute
*

Every Hour
*
Every Other Minute
*/2

Every Other Hour
*/2
Every Five Minutes
*/5

Every Five Hours
*/5
1st minute
1

12 at noon
12
2nd minute
2

6 AM
6
58th minute
58

10 at night
22
59th minute
59

12 in morning
0
Day(s)(1-31)
Value

Month(s)(1-12)
Value
Everyday
*

Every Month
*
1st Day
1

January
1
2nd Day
2

February
2
Day 30
30

December
12
Day 31
31






Weekdays( 0-6)
Value



Every Weekday
*



Sunday
0



Monday
1



Friday
5



Saturday
6


Notes


  1. The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges or two numbers in the range separated by a hyphen (meaning an inclusive range).
  2. Repeat pattern like /2 for every 2 minutes or /10 for every 10 minutes is not supported by all operating systems. If you try to use it and crontab complains it is probably not supported.
  3. The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .
  4. If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead, exit with Control-c.
  5. Script names are case sensitive on Unix. So be careful in entering the script name.
  6. If you don't wish to specify a value for a field, just place a * in the field.
  7. When using the names of weekdays or months, it isn't case sensitive, but only the first three letters should be used, e.g. Mon, sun or Mar, jul.
  8. Comments are allowed in crontabs, but they must be preceded with a '#', and must be on a line by them self.
  9. The cronjob which is set and saved for the user arjun gets saved inside the file /var/spool/cron/arjun.
  10. If you wish to use more than one instance of a particular time periods, then seperate the times by a comma. If you wish for continuous execution, the start and stop items are separated by a dash. For example, if you wanted to run your command at :05 and :35 past the hour, every hour, Monday through Friday, then your time stamp would look like this:

5,35 * * 1-5 your_command

The fifth position indicates which task will be run at the given time(s).