Cron


Table of Contents

Linux Help on cron 
Getting started with cron 
Scheduling automatic backups with cron 
More cron tricks: Time ranges 
cmd:Vixie 
Putting a skeleton in every user's closet 
cmd:crontab 
Usage 
Configuring a Cron Task in RedHat 
Help 
Cron jobs testing 
Cron jobs testing 
Cron jobs testing 
Cron jobs testing 
cron jobs without mailing 
cron jobs without mailing 
Limitation of cron 
Limitation of cron 
Limitation of cron 
[Solved] grep and cron & command line 
cron 
sh script cron job 
sh script cron job 
sh script cron job 

Linux Help on cron 

http://www.csun.edu/helpdesk/linux.htm

Getting started with cron 

The cron daemon is a truly nifty thing. Throughout the day (and night, if you leave your system running), it wakes up every minute, and checks /etc/crontab to see whether it needs to perform any tasks. If so, the utility executes them. It's as if you had a properly-trained robot typing commands at your keyboard. Cron is ideal for tasks that need to be carried out regularly—performing incremental backups is definitely one of them.

The cron daemon consults lots of system-level files, including several in /etc/cron, but each user can set up an individually tailored cron. To create your own cron, you'll need to use the crontab utility, which lets you create the crontab file and then installs this file correctly.

To create the crontab file, you need to type a command line. The syntax is simple. The first five fields of the line, separated by spaces, correspond to the following times:

  • Minutes (0-59)
  • Hours (0-23)
  • Days (1-31)
  • Months (1-12)
  • Days of the week (0-6, or Sun, Mon, Tue, etc.)

If you type an asterisk in one of these fields, it means "all the times for this field." Following the five time fields is a command, just as if you'd type it at the keyboard. Here's an example: 15 * * * * echo "Hello, there"

This command tells cron to send the message "Hello, there," every fifteen minutes. Here's another example:

0 6 * * * smbmount //rivendell/backup /home/bryan/backup -N

This command tells cron to execute the smbmount command every morning at 6:00 A.M. sharp.

To create a crontab file, log in using your ordinary user account, type cronttab -e, and press [Enter]. You'll see your default editor. Type the crontab commands you want to use, and exit the editor. Crontab will create your crontab file, and update your system so that the commands execute at the times you specified.

Scheduling automatic backups with cron 

If you've learned the basics of the cron daemon and the crontab utility, you can use cron to schedule automatic incremental backups with the tar utility, as described in "Develop a backup routine" (Mar. 23, 2000). Here's the command to perform an automatic, incremental backup of an entire home directory (/home/bryan, in this example); note that this command is scheduled to run late every night, to be precise. (In case you've forgotten, cron commands begin with five time fields, corresponding to minute, hour, day of month, month, and day of week.)

59 23 * * * tar -uf /home/bryan/backup/backup.tar /home/bryan

The tar command updates the archive (backup.tar) with all the files that have been created or modified since the last incremental backup occurred.

More cron tricks: Time ranges 

You can also specify time ranges with cron. To specify a list of times, specify them in a comma-separated list:

0 15,18,23 * * * rm /home/suzanne/core

This command removes core files from the specified directory at 3, 6, and 11 P.M. (the times are indicated using the 24-hour military clock).

To specify a range of times, use a hyphen. The following runs the specified tar command Monday through Friday, but gives tar a break on weekends:

  • 23 * * mon-fri tar -uf backup.tar /home/suzanne

To configure the user's cron settings, use crontab with the -e switch (crontab -e).

cmd:Vixie 

What's Vixie, you wonder? It's a nifty addition to the cron system, and it's installed by default with most Linux distributions (NB, default in RedHat and Debian). To find out whether you've got Vixie, take a look at your /etc directory; if you see directories such as /etc/cron.hourly, /etc/cron.daily, and so on, you've got Vixie.

And what does Vixie do? Essentially, Vixie is a scripting interface to cron. Vixie does its magic with a script called run-parts, which lives by default in the /usr/bin directory. This script looks for special commands in /etc/crontab, the system administrator's cron configuration file. These commands tell the run-parts script which directory to look in. At the specified time, the script examines this directory and executes all the scripts it finds.

Take a look in the /etc/crontab.* directories. Chances are you'll find lots of these scripts. If you add additional scripts to these directories, Vixie will execute them at the interval specified by the directory name (such as /etc/crontab.weekly) and the specific time given in /etc/crontab.

Putting a skeleton in every user's closet 

When you create a new user for your Linux system, most of the utilities used for this purpose (including Linuxconf and adduser) automatically create a new home directory for the user. What's more, these utilities also copy to the new home directory all the files that are found in /etc/skel. If you add files to those found in this directory, or change the files found in this directory, your additions and changes will automatically appear in every new user's directory. For example, suppose you'd like to give every user the benefit of a custom bash prompt you've devised. If you make the needed changes to the bash configuration files, every new user will see the customized bash prompt instead of the default. Note, however, that changes to /etc/skel don't affect existing directories; you'll have to change those manually.