Using Crontab in UNIX

1. Controls on Using the crontab Command

The /var/adm/cron/cron.allow and /var/adm/cron/cron.deny files control which users can use the crontab command A root user can create, edit, or delete these files.


Entries in these files are user login names with one name to a line. If your login ID is associated with more than one login name, the crontab command uses the first

login name that is in the /etc/passwd file, regardless of which login name you might actually be using. Also, to allow users to start cron jobs, the daemon attribute in

the /etc/security/user file should be set to TRUE , using the chuser command.


The following is an example of an cron.allow file:


root

nick

dee

sarah


If the cron.allow file exists, only users whose login names appear in it can use the crontab command. The root user's log name must appear in the cron.allow file if the

file exists. A system administrator can explicitly stop a user from using the crontab command by listing the user's login name in the cron.deny file. If only the

cron.deny file exists, any user whose name does not appear in the file can use the crontab command.


A user cannot use the crontab command if one of the following is true:

* The cron.allow file and the cron.deny file do not exist (allows root user only).

* The cron.allow file exists but the user's login name is not listed in it.

* The cron.deny file exists and the user's login name is listed in it.


If neither the cron.allow nor the cron.deny file exists, only someone with root user authority can submit a job with the crontab command.




2. Crontab Commands

export EDITOR=vi ;to specify a editor to open crontab file.


c -e UserName

Edits a copy of the user's crontab file or creates an empty file to edit if the crontab file does not exist for a valid UserName. When editing is complete, the

file is copied into the crontab directory as the user's crontab file.

-l UserName

Lists the user's crontab file.

-r UserName

Removes the user's crontab file from the crontab directory.

-v UserName

Lists the status of the user's cron jobs.


3. Crontab file


Crontab syntax :-

A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

* * * * * command to be executed

- - - - -

| | | | |

| | | | +----- day of week (0 - 6) (Sunday=0)

| | | +------- month (1 - 12)

| | +--------- day of month (1 - 31)

| +----------- hour (0 - 23)

+------------- min (0 - 59)



* in the value field above means all legal values as in braces for that column.

The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).


Note: 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. Crontab Example



2 To write the time to the console every hour on the hour, enter:


0 * * * * echo The hour is `date` .

>/dev/console

3 To run the calendar command at 6:30 a.m. every Monday, Wednesday, and Friday, enter:


30 6 * * 1,3,5 /usr/bin/calendar


4 To run the calendar command every day of the year at 6:30, enter the following:


30 6 * * * /usr/bin/calendar

5 To run a script called maintenance every day at midnight in August, enter the following:


0 0 * 8 * /u/harry/bin/maintenance

6 To define text for the standard input to a command, enter:


0 16 * 12 5 /usr/sbin/wall%HAPPY HOLIDAY!%Remember to

turn in your time card.

The text following the % (percent sign) defines the standard input to the wall command as:


HAPPY HOLIDAY!


Remember to turn in your time card.


5. Crontab Environment

cron invokes the command from the user's HOME directory with the shell, (/usr/bin/sh).

cron supplies a default environment for every shell, defining:

HOME=user's-home-directory

LOGNAME=user's-login-id

PATH=/usr/bin:/usr/sbin:.

SHELL=/usr/bin/sh


Users who desire to have their .profile executed must explicitly do so in the crontab entry or in a script called by the entry.


6. Disable Email


By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .


>/dev/null 2>&1


7. Generate log file


collect the cron execution execution log in a file :
30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log

Number of Hits : Hit Counter by Digits

ADD this Info

Bookmark and Share