Web Server Manual | Frames Format

To Search this page type CTRL-f or CMD-f key now.
To Search all pages, enter your question here:


Crontab FAQs

 

Overview of the cron daemon | top

It is a method of automating tasks performed on a regular basis.

The cron daemon is where all timed events are initiated. It is executed upon system initialization and remains active while the system is operating in multi-user mode. Cron wakes up every minute and examines all the stored configuration files, called crontabs, to check each them for commands that may be scheduled to be executed at the current time.

Besides starting commands each minute, some cron daemons also check to see if its spool directory's last modified time has been updated. If it has, cron will check the modification time on all crontabs and reread the ones that have been modified. Other cron daemons examine new crontab files when first initialized and when the commands crontab or at are executed. This reduces the overhead of checking for new or changed files at regularly scheduled intervals.

The cron / crontab file format (Server Admin Notes)

Cron searches the crontab spool directory for crontab files. These files are named after user accounts. For instance, if the system administrator is logged into the root accounts creates a crontab file, it will be named root and will be placed in the crontab spool directory. If Bill Wilson, whose username is bill, creates a crontab file it is named bill in the crontab spool directory. 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).

The configuration files used to control the operation of cron are called crontab files or cron tables. These files contain information about the time, date and command to execute.

 /var/spool/cron               main cron directory 
 /var/spool/cron/cron.allow    grant access to the cron facility 
 /var/spool/cron/cron.deny     revoke access to the cron facility

Cron table files, or crontabs, are text files which direct the cron daemon's activity. Each line or entry has six fields which are separated by space characters. The first five fields instruct the cron daemon as to when to execute the command, which is contained in the sixth field.

- Try this crontab generator.

                  
FIELD    VALUE
------------------
minute   00 to 59 
hour     00 to 23 (military time) 
day      1 to 31 
month    1 to 12 
weekday  0 to 6 (0=Sunday) Note: Linux uses sun, mon...
                  

The first five fields can also use any one of the following formats.

Here are sample entries along with a short explanation of when the operation will be performed.

0 * * * * echo "WAKE UP" 2>&1 /dev/console
                  

This entry sends the string WAKE UP to the device /dev/console at the start of every hour on every day of every month.

0 0 * * *   calendar -
                  

This entry runs the command calendar which reminds users of holidays and other events at the start of the hour at the start of the day on every day of the month.

10,20,30,40,50 * * * *  /adm/checkdaemon 2>&1 | /bin/mail -s "CRON:Check" root
                  

This entry runs the command checkdaemon and mails the output of the command to root. The command is run 10, 20, 30 ,40, and 50 minutes after the hour on every day of every month.

The crontab commands

The crontab files are not generated by editing a the crontab file in the crontab spool directory, instead the command crontab is used to edit, list, create or remove a crontab file for a user. The crontab command can be used by all the users on a system to create personal crontab as well as by the root account. Users are not allowed to view, edit or create crontab files for other users. Basic commands:

crontab -e
The edit option crontab -e for the crontab command copies or creates the current user's crontab file. After editing is complete, the file is installed as the user's crontab file in the crontab spool directory.

crontab -l
The list option, crontab -l, displays the contents of the current user's crontab file.

crontab -r
The remove option, crontab -r, empties the contents of the current user's crontab file.

Quick Crontab Editing:

The first step is to open a local text editor (such as, write, bbedit, etc).

Switch OFF word wrap if it has this feature.

Next, type in the commands.
The example below executes a web browser call to page mail.php3, every 15 minutes.

00 * * * * wget -q -O /dev/null -nd http://tvcnet.com/services/reminders/mail.php3
15 * * * * wget -q -O /dev/null -nd http://tvcnet.com/services/reminders/mail.php3
30 * * * * wget -q -O /dev/null -nd http://tvcnet.com/services/reminders/mail.php3
45 * * * * wget -q -O /dev/null -nd http://tvcnet.com/services/reminders/mail.php3
                  

Now press return again so you have a blank line at the bottom. THIS IS VERY IMPORTANT

  1. Save the file as a text file, such as, mycron.txt
  2. FTP to your account, and upload this file as type text to your /home/vs##### directory.
  3. Now telnet to your account and at the command prompt type

    crontab -l       (note lowercase L )

    crontab mycron.txt 

Now, try crontab -l again.

Congratulations. You've created your first crontab!

Example used with log files:

Create a text file with the example contents below, edit as necessary and upload to home directory (crontab executes it, see below):

#!/bin/sh 
cp vs00000.access.log vs00000.access_old.log
cp empty.file vs00000.access.log
cp vs00000.error.log vs00000.error_old.log
cp empty.file vs00000.error.log

 

Set permissions of this shell script file to 755.

Then include this line (modify the vs00000 info) in the crontab file:

01 00 * * mon /home/vs00000/shell_script_file_name

This archives the data and resets the logs 12:01AM every Monday.



 

Top Index