cron is a time-based job scheduler in Linux operating systems. cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate 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.
To start the cron service all you need to do is:
# /etc/init.d/cron start
OR
#start service cron
cron searches its spool area (/var/spool/cron/crontabs) for crontab files. A crontab file contains instructions to the cron daemon of the general form: "run this command at this time on this date". Each user has their own crontab, and commands in any given crontab will be executed as the user who owns the crontab. crontabs found are loaded into memory.
cron then wakes up every minute, examining all stored crontabs, checking each command to see if it should be run in the current minute. When executing commands, any output is mailed to the owner of the crontab (or to the user named in the MAILTO environment variable in the crontab, if such exists).
Along with the crontab files, there is also a command of the same name – crontab. crontab is the program used to install, remove or list the tables used to drive the cron. Check out the crontab(1) man page to refer the list of options that the crontab command provides (click here).
Crontab
Format
Commands are executed by cron when
the minute, hour, and month of year fields match the current time, and when at
least one of the two day fields (day of month, or day of week) match the
current time.
A field may be an asterisk (*),
which always stands for "first-last".
Ranges of numbers are allowed.
Ranges are two numbers separated with a hyphen. The specified range is
inclusive. For example, 8-11 for an "hours" entry specifies execution
at hours 8, 9, 10 and 11.
Lists are allowed. A list is a set
of numbers (or ranges) separated by commas. Examples: "1,2,5,9",
"0-4,8-12".
Step values can be used in
conjunction with ranges. Following a range with "/" specifies skips
of the number's value through the range. For example, "0-23/2" can be
used in the hours field to specify command execution every other hour. Steps
are also permitted after an asterisk, so if you want to say "every two
hours", just use "*/2".
Names can also be used for the
"month" and "day of week" fields. Use the first three
letters of the particular day or month (case doesn't matter). Ranges or lists
of names are not allowed.
The "sixth" field (the
rest of the line) specifies the command to be run. The entire command portion
of the line, up to a newline or % character, will be executed by /bin/sh or by
the shell specified in the SHELL variable of the crontab file. Percent-signs
(%) in the command, unless escaped with backslash (\), will be changed into
newline characters, and all data after the first % will be sent to the command
as standard input.
Here are some examples of crontab
lines. Use the command "crontab -e" to edit your crontab file.
This line executes the "ping" command every minute
of every hour of every day of every month. The standard output is redirected to
dev null so we will get no e-mail but will allow the standard error to be sent
as an e-mail
*
* * * * /sbin/ping -c 1 192.168.0.1 >
/dev/null
This line executes the "ping" and the "ls" command every 12am and 12pm on the 1st day of every 2nd month. It also puts the output of the commands into the log file /var/log/cronrun.
This line executes the "ping" and the "ls" command every 12am and 12pm on the 1st day of every 2nd month. It also puts the output of the commands into the log file /var/log/cronrun.
0,12 1 */2 * /sbin/ping -c
192.168.0.1; ls -la >>/var/log/cronrun
This line executes the disk usage command to get the directory sizes every 2am on the 1st through the 10th of each month. E-mail is sent to the email addresses specified with the MAILTO line. The PATH is also set to something different.
PATH=/usr/local/sbin:/usr/local/bin:/home/user1/bin
MAILTO=user1@nowhere.org,user2@somewhere.org
0
2 1-10 * * du -h --max-depth=1 /
Joohi Sinha
Stay tuned for more.
Thankss for sharing such a wonderful information......
ReplyDelete