Pacific Passion
Thought could post a few details about cron for the benefit of some of my
friends. A cron is a utility used for scheduling jobs on unix environment.
It’s used to automate a system administrator’s work. If you are good at
writing a shell script you can automate most of the administration tasks
like archiving logs, checking for stuck threads, Exceptions and errors in
log files, Listener port status etc. Crontab is the configuration file that
specifies the shell commands to run periodically. Cron is set up as a
daemon in unix which constantly checks the /etc/crontab file and
/var/cron/tans directory for new crontab files. Crontab file stores
information about the task the cron is supposed to perform.
Each line in a crontab represents a job, the cron expression and the
command to execute. You can also instruct the cron to run a shell script at
specified time.

Crontab Syntax:*    *    *    *    *  command to be executed
┬    ┬    ┬    ┬    ┬
│    │    │    │    │
│    │    │    │    │
│    │    │    │    └───── day of week (0 - 7) (Sunday=0 or 7)
│    │    │    └────────── month (1 - 12)
│    │    └─────────────── day of month (1 - 31)
│    └──────────────────── hour (0 - 23)
└───────────────────────── minutes (0 - 59)
<> <> <>
Name    Mandatory Values    Special
 MinutesYes0-59* / , -
HoursYes0-23* / , -
Day of the MonthYes01/31/11* / , - ? L W
MonthYes1-12 or JAN - DEC* / , -
Day of the weekYes0-6 or SUN - SAT* / , - ? L #
YearNo1970-2099* / , -


Special characters and their meaning
  *     All values
  /     Repeat pattern like /2 for every 2 minutes or /10 for every 10
     minutes
  ,     Separate items eg “MON,WED,FRI"
  -    Used to specify range, Range specified is inclusive, Lists are also
     allowed.  A list is a set of numbers (or ranges) separated by commas,
     Eg: “1, 5,9” or “1-3, 8-11”
  ?    Used to omit the value
  L    Stand for Last
  W   Stands for Weekday (Mon ? Fri)
  #     is allowed for day of week. Allows specification like 2nd Friday
     of the month

Other Key Points:  - Blank Lines, Leading spaces and tabs are ignored
  - Lines beginning with pound sign (#) are considered as comments, Comments
     are not allowed on the same line of the command or environment
     variable. If it’s found on the same line they would be treated as
     part of command.
  - The specification of days can be made in two fields: month day and
     weekday. If both are specified in an entry, then cumulative meaning
     of both the entries will get executed
  - Names can also be used for the ``month'' and ``day of week'' fields.
     The first three letter of the day or month are used case doesn't
     matter.
  - The last field i.e 6th specifies the command to be run.  The rest of the
     line after the 5th field is considered as part of the command until a
     newline or % character is found. 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.

     Eg:    ``30 4 1,30 * 4'' would cause a command to be run at  4:30 am
     on the 1st and 30th of each month, plus every Thursday.

Crontab Commands:export EDITOR=vi  To specify a editor to open crontab file.
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 -u      To change other users crontab, Only privilleged users can use this
crontab -v      Display the last time you edited your crontab file.

Other Config FilesThe following two files play an important role:

1. /etc/cron.allow - If this file exists, your user id must me listed in order
  to have the privileges to run the cron jobs.

2. /etc/cron.deny - If the cron.allow file does not exist but the
  /etc/cron.deny file does exist, then your id must not be listed in the
  /etc/cron.deny file for you to run the cron jobs.

Environment Variables: SHELL is set to /bin/sh, and LOGNAME and HOME  are
set  from  the  /etc/passwd

By Defaults if MAILTO is defined (and non-empty), mail is sent to the
user/DL so named.   If MAILTO is defined but empty (MAILTO=""), no mail
would be sent.  Otherwise mail is sent to the owner of the crontab

If no email is required then end of the cron job line with the below
command
>/dev/null 2>&1
0 Responses

Post a Comment

Translate

Total Pageviews

Pacific Passions. Powered by Blogger.

Followers

Popular Posts

Digital Tech News - Digital Technology & Consumer Electronics

BBC News - Technology

World Wide Words newsletter

Techbargains.com

Lifehacker