Monday, February 2, 2009

cron command

the name of the cron daemon is crond.

crontab Entries

A crontab entry has six fields: the first five are used to specify the time for an action, while
the last field is the action itself.

• The first field specifies minutes (0–59).
• The second field specifies the hour (0–23).
• The third field specifies the day of the month (1–31).
• The fourth field specifies the month of the year (1–12, or month prefixes like Jan and Sep).
• The fifth field specifies the day of the week (0–6, or day prefixes like Wed and Fri), starting with 0 as Sunday.

syntax: minute hour day-month month day(s)-week task

Ex:

0 2 * * 1-5 tar cf /home/backp /home/projects
0 2 * * Mon-Fri tar cf /home/backp /home/projects
0 2 * * 0,3,5 tar cf /home/backp /home/projects
0 2 * * Mon-Fri tar cf /home/backp /home/projects

The cron.d Directory

On a heavily used system, the /etc/crontab file can easily become crowded. There may also be instances in which certain entries require different variables. For example, you may need to run some task under a different shell. To help you organize your crontab tasks, you can place crontab entries in files within the cron.d directory. The files in the cron.d directory all contain crontab entries of the same format as /etc/crontab. They may be given any name. They are treated as added crontab files, with cron checking them for tasks to run.


The crontab command

The crontab command takes the contents of the text file and creates a crontab file in the /var/spool/cron directory, adding the name of the user who issued the command. In the following example, the root user installs the contents of mycronfile as the root’s crontab file:

sudo crontab mycronfile

This creates a file called /var/spool/cron/root. If a user named justin installs a crontab file, it creates a file called /var/spool/cron/justin. You can control use of the crontab command by regular users with the /etc/cron.allow file. Only users whose names appear in this file can create crontab files of their own. Conversely, the /etc/cron.deny file lists those users denied use of the cron tool, preventing them for scheduling tasks. If neither file exists, access is denied to all users. If a user is not in an /etc/cron.allow file, access is denied. However, if the /etc/cron.allow file does not exist, and the /etc/cron.deny file does, then all users not listed in /etc/cron.deny are automatically allowed access.


Editing in cron

Never try to edit your crontab file directly. Instead, use the crontab command with the -e option. This opens your crontab file in the /var/spool/cron directory with a standard text
editor, such as Vi (crontab uses the default editor as specified by the EDITOR shell environment variable).




No comments:

Post a Comment