Contenues dans
Trouver plus de documentation
Ressources d'assistance comprises
| Télécharger cet ouvrage au format PDF
Using crontab
6
- This chapter describes how to use the crontab command to submit commands to be run at a later time; how to edit, display, and remove crontab files; and how to restrict access to the crontab facility. This chapter contains the following sections:
-
Overview
- There are two parts to the crontab facility.
-
- The crontab command allows you to submit commands to be run later. You can also have the command run at regular intervals.
- The cron command starts the cron daemon, which runs the commands submitted with crontab.
- You can use the crontab facility to automate tasks such as backups or other time consuming tasks.
The crontab Command
- To submit a command to be executed later, you use the crontab command to create a crontab file. The crontab file specifies what the command is, when it should be run, and how often. See "The Format of crontab Files" on page 58 for a description of crontab files. See "Creating crontab Files" on page 59 for instructions to create a crontab file.
- The at(1) command can be used to submit a command that will only be executed once. See the at(1) manual page for more information.
The cron Command
- The cron command is normally executed by an /sbin/rc2.d script at boot time. The cron daemon executes the commands in the crontab file at the specified times. See the cron(1M) manual page for more information on the cron daemon.
The Format of crontab Files
- A line in a crontab file consists of the following six fields, which are separated by spaces:
-
minute hour day_of_the_month month day_of_the_week command
- The fields have the following meanings:
-
-
minute: The minute at which to execute the specified command, ranging from 0, for on the hour, to 59, for one minute before the hour
-
hour: The hour, based on a 24-hour clock, at which to execute the specified command, ranging from 0, for midnight, to 23, for 11 p.m.
-
day_of_the_month: The day of the month on which to execute the specified command, ranging from 1 to 31
-
month: The month of the year during which to execute the command, ranging from 1 to 12
-
day_of_the_week: The day of the week to execute the specified command, ranging from 0 (for Sunday) to 6 (for Saturday)
-
command: The command that should be run at the specified time
- An asterisk indicates a field is left blank.
- Note that the day can be specified by two of the fields (day_of_the_month and day_of_the_week). If both are specified, both will be used. If one of the two fields is an asterisk, only the other is used.
- If the first five fields of a crontab line were:
-
-
0 0 15,30 * 1
- the command would be run at midnight on the fifteenth and thirtieth of each month and on every Monday. To specify only specific days of the week to run a command, put an asterisk (*) in the day_of_the_month field. To specify only days of the month, put an asterisk (*) in the day_of_the_week field.
- If you do not direct the standard output and standard error for the command, any output for standard output or standard error is mailed to you.
- Any line in a crontab file that begins with a hash mark (#) is a comment and is ignored.
Creating crontab Files
· How to Create a crontab File
-
-
Type crontab and press Return.
The crontab command expects the crontab file information from standard input.
-
Type the crontab line and press Return.
-
Press Control-d to indicate the end of standard input.
- You can also create a crontab file by using a text editor to create a file that contains the crontab information and then specifying the name of the file as an argument to the crontab command.
- To create a crontab file for the superuser, you must first become superuser and then create a crontab file.
Editing crontab Files
- Use the -e option to crontab to edit crontab files. The environment variable EDITOR determines which editor is used. If the EDITOR environment variable has not been set, the default editor ed(1) is used.
- You must be the superuser to edit the superuser's crontab file, or to edit a crontab file that belongs to another user.
· How to Edit Your Own crontab File
-
* Type crontab -e and press Return.
· How to Edit the Superuser's crontab File
-
-
Become superuser.
-
Type crontab -e and press Return.
· How to Edit Another User's crontab File
-
-
Become superuser.
-
Type crontab -e username and press Return.
Displaying crontab Files
- You must be superuser to display the contents of the superuser's crontab file or to display a crontab file that belongs to another user.
- Use the -l option to the crontab command to display a crontab file.
-
Caution - If you inadvertently enter the crontab command with no argument, do not attempt to exit by pressing Control-d which would remove all entries in the crontab file. Instead, exit by pressing the interrupt character (usually Control-c).
· How to Display Your Own crontab File
-
* Type crontab -l and press Return.
· How to Display the Superuser's crontab File
-
-
Become superuser.
-
Type crontab -l and press Return.
· How to Display a User's crontab File
-
-
Become superuser.
-
Type crontab -l username and press Return.
Removing crontab Files
- You must be superuser to remove a crontab file that belongs to another user or to remove the superuser's crontab file.
- Use the -r option to the crontab command to remove a crontab file.
· How to Remove Your Own crontab File
-
* Type crontab -r and press Return.
· How to Remove the Superuser's crontab File
-
-
Become superuser.
-
Type crontab -r and press Return.
· How to Remove a User's crontab File
-
-
Become superuser.
-
Type crontab -r username and press Return.
Controlling Access to crontab
- The cron.allow and cron.deny files are used to control access to the crontab file. Both cron.allow and cron.deny are text files that you create and modify with a text editor. Both files are found in the directory /etc/cron.d.
- The cron.allow file contains the names of users that are allowed to submit crontab files. The cron.deny file contains the names of users that cannot submit crontab files. Table 6-1 describes how to allow access to the crontab command in different cases.
-
Table 6-1 cron.denycron.allow
| To Allow These People to Submit crontab Files... | Do the Following |
| Superuser only | Delete (or do not create) cron.deny and cron.allow |
| All users | Create an empty cron.deny file. Do not create cron.allow. |
| No users, with a few exceptions | Create a cron.allow file containing a list of users who are allowed to submit crontab files. Do not create cron.deny. |
| All users, with a few exceptions | Create a cron.deny file containing a list of all users who cannot submit crontab files. Do not create cron.allow. |
- The cron.deny file is installed as part of the SunOS installation and the cron.allow file should be created by the superuser to extend access to the crontab command.
- The following example shows the default cron.deny file:
-
daemon
bin
smtp
nuucp
listen
nobody
noaccess
|
|
|