-->
Previous | Table of Contents | Next |
The date and time listed are the date and time you pressed <Ctrl-d> to complete the batch command. When the job is complete, check your mail; anything that the commands normally display is mailed to you.
Both at and batch schedule commands on a one-time basis. To schedule commands or processes on a regular basis, you use the cron program. You specify the times and dates you want to run a command in crontab files. Times can be specified in terms of minutes, hours, days of the month, months of the year, or days of the week.
The cron program is started only once, when the system is booted. Individual users shouldnt have permission to run cron directly. Also, as the system administrator, you shouldnt start cron by typing the name of the command; cron should be listed in a shell script as one of the commands to run during a system boot-up sequence.
When started, cron (short for chronograph) checks queues for at jobs to run and also checks to see whether users or the root have scheduled jobs by using crontab files. If theres nothing to do, cron goes to sleep and becomes inactive; it wakes up every minute, however, to see if there are commands to run. You can see how important and useful this facility is; also, cron uses very few system resources.
Use crontab to install a list of commands that will be executed on a regular schedule. The commands are scheduled to run at a specified time (such as once a month, once an hour, once a day, and so on). The list of commands to be performed on the specified schedule must be included in the crontab file, which is installed with the crontab command. After you install the crontab file, cron reads and executes the listed commands at the specified times. Also with the crontab command, you can view the list of commands included in the file and cancel the list if you want.
Before you install your crontab file with the crontab command, create the file containing the list of commands you want to schedule by using a text editor such as vi or emacs. The crontab command handles the placement of the file. Each user has only one crontab file, created when the crontab command is issued. This file is placed in a directory thats read by the cron command.
Linux stores the users crontab file in the /usr/spool/cron/crontabs directory and gives the file the users name. If your user name is mcn and you use a text editor to create a file called mycron and install it by typing crontab mycron, the file /usr/spool/cron/crontabs/mcn is created. (In this example, the mcn file is created, or overwritten, with the contents of mycron, which may contain entries that launch one or more commands.)
NOTE: For users to use the crontab command, they must be listed in the /etc/cron.d/cron.allow file. If you add a user to the system from the command line (by using the useradd command), he or she isnt added automatically to the /etc/cron.d/cron.allow file. As the root user, you must add the new user to the cron.allow file with a text editor.Although you can initially create your crontab file with a text editor, after you create your crontab file, modify it by using only the crontab command. Dont try to replace or modify the file that cron examines (that is, the /usr/spool/cron/crontabs/user file) by any means other than by using the crontab command.
Each line in the crontab file contains a time pattern and a command. The command is executed at the specified time pattern. The time pattern is divided into five fields separated by spaces or tabs. Any output that usually appearsthat is, information that isnt redirected to stdout or stderr is mailed to the user.
Following is the syntax for the commands you enter in a file to be used by crontab:
minute hour day-of-month month-of-year day-of-week command
The first five fields are time option fields. You must specify all five of these fields. Use an asterisk (*) in a field if you want to ignore that field.
NOTE: Technically, an asterisk in a crontab field means any valid value instead of ignore the valuethat is, match anything. The crontab entry 02 00 01 * * date, for example, says to run the date command at two minutes after midnight (zero hour) on the first day of the month. Because the month and day of the week fields are both asterisks, this entry runs on the first day of every month and any day of the week that the first of the month happens to land on.
Table 19.4 lists the time-field options available with crontab.
Field | Range |
---|---|
minute | 00 through 59 |
hour | 00 through 23 (midnight is 00) |
day-of-month | 01 through 31 |
month-of-year | 01 through 12 |
day-of-week | 01 through 07 (Monday is 01, Sunday is 07) |
You can have as many entries as you want in a crontab file and can designate them to run at any time. This means that you can run as many commands as you want in a single crontab file.
To sort a file named /usr/wwr/sales/weekly and mail the output to a user named twool at 7:30 a.m. each Monday, use the following entry in a file:
30 07 * * 01 sort /usr/wwr/sales/weekly |mail -sWeekly Sales twool
This command specifies the minute as 30, the hour as 07, any day of the month with the asterisk, any month of the year with another asterisk, and the day-of-week as 01 (which represents Monday).
Previous | Table of Contents | Next |