Planifier des tâches
Contents
1 Introduction
Use the at command to automatically execute a job only once at a specified time.
2 at
The format for the at command is:
at |
at -m -q queuename time date at -r job at -l |
The table shows the options you can use to instruct the cron process on how to execute an at job.
Option | Description |
---|---|
-m | Sends mail to the user after the job has finished |
-r job | Removes a scheduled at job from the queue |
-q queuename | Specifies a specific queue |
time | Specifies a time for the command to execute |
-l | Reports all jobs scheduled for the invoking user |
date | Specifies an optional date for the command to execute, which is either a month name followed by a day number or a day of the week |
For example, to create an at job to run at 9:00 p.m. to locate and verify the file type of core files from the /export/home directory, perform the command:
at |
# at 9:00 pm at> find /export/home -name core -exec file {} \; >> /var/tmp/corelog at> <EOT> commands will be executed using /sbin/sh job 1098648000.a at Mon Oct 25 21:00:00 2004 |
To display information about the execution times of jobs, perform the command:
at |
# at -l 1098648000.a 1098648000.a Mon Oct 25 21:00:00 2004 |
To display the jobs queued to run at specified times by chronological order of execution, perform the command:
# atq Rank Execution Date Owner Job Queue Job Name 1st Oct 25, 2004 21:00 root 1098648000.a a stdin |
To view all the at jobs currently scheduled in the queue, perform the command:
# ls -l /var/spool/cron/atjobs total 4 -r-Sr--r-- 1 root root 1044 Oct 25 13:48 1098648000.a |
You can also use the at command to remove a job from the at queue.
For example, to remove job 1098648000.a from the at queue, perform the command:
# at -r 1098648000.a # atq Rank Execution Date Owner Job Queue Job Name |
As the root user, you control who has access to the at command with the at.deny and at.allow files.
- The /etc/cron.d/at.deny File
By default, the Solaris OS includes the /etc/cron.d/at.deny file. This file identifies users who are prohibited from using the at command. The file format is one user name per line. The file initially contains:
daemon bin nuucp listen nobody noaccess
A user who is denied access to the at command receives the following message when attempting to use the command:
at: you are not authorized to use at. Sorry. |
If only the /etc/cron.d/at.deny file exists but is empty, then all logged-in users can access the at command.
- The /etc/cron.d/at.allow File
The /etc/cron.d/at.allow file does not exist by default, so all users (except those listed in the /etc/cron.d/at.deny file) can create at jobs. By creating the /etc/cron.d/at.allow file, you create a list of only those users who are allowed to execute at commands.
The /etc/cron.d/at.allow file consists of user names, one per line.
The interaction between the at.allow and the at.deny files follows these rules:
- If the at.allow file exists, only the users listed in this file can execute at commands.
- If the at.allow file does not exist, all users, except for users listed in the at.deny file, can execute at commands.
- If neither file exists, only the root user can use the at command.
- If a user is listed in both files, the user is denied.
3 cron
You can use the cron facility to schedule regularly recurring commands. Users can submit a command to the cron facility by modifying their crontab file.
All crontab files are maintained in the /var/spool/cron/crontabs directory and are stored as the login name of the user that created the cron job.
The cron daemon is responsible for scheduling and running these jobs.
Note: The clock daemon, cron, starts at system boot and runs continuously in the background.
A crontab file consists of lines of six fields each. The fields are separated by spaces or tabs. The first five fields provide the date and time the command is to be scheduled. The last field is the full path to the command.
Note: If the command field contains a percent (%) character, then all subsequent characters are passed to the command as standard input.
These first five fields are separated by spaces and indicate when the command will be executed. See the image.
The first five fields follow the format rules shown in the table.
Value | Rule | Example |
---|---|---|
n | Matches if field value is n | As shown in the preceding figure for hour or minute, a 3 or 10 |
n,p,q | Matches if field value is n, p, or q | Every 10 minutes would be represented by 0,10,20,30,40,50 |
n-p | Matches if field has values between n and p inclusive | The hours between 1:00 a.m. and 4:00 a.m. would be represented by 1-4 |
* | Matches all legal values | As in the preceding example for the month, representing every month. |
The crontab command enables the user to view, edit, or remove a crontab file.
3.1 Viewing a crontab File
To view the contents of the root crontab file, run the crontab -l command as the root user.
This is the same command that users run to view the contents of their own crontab file.
As the root user, you can view the contents of any regular user's crontab file by performing the command:
crontab -l username |
3.2 Editing a crontab File
Caution: If you accidentally enter the crontab command on the command line without an option (-l, -e, -r), press the interrupt keys Control-C to exit. Do not press Control-D, this action overwrites the existing crontab file with an empty file.
To create or edit a crontab file, follow these steps:
- Check that the EDITOR variable is set to the editor you want to use. This instructs the cron utility which editor to use to open the file.
EDITOR=vi export EDITOR |
- Run the following crontab command to open your crontab file, and add the appropriate entry.
crontab -e 30 17 * * 5 /usr/bin/banner "Time to go!" > /dev/console :wq |
Note: If the users do not redirect the standard output and standard error of their commands in the crontab file, any generated output or errors are mailed electronically to the user.
3.3 Removing a crontab File
The correct way to remove a crontab file is to invoke the command:
crontab -r username |
Typical users can remove only their own crontab file. The root user can delete any user's crontab file.
You can control access to the crontab command with two files in the /etc/cron.d directory--the cron.deny file and the cron.allow file.
These files permit only specified users to perform crontab tasks, such as creating, editing, displaying, or removing their own crontab files.
3.4 The /etc/cron.d/cron.deny File
The Solaris OS provides a default cron.deny file. The file consists of a list of user names, one per line, of the users who are not allowed to use cron. The following is an example of the contents of a cron.deny file:
daemon bin nuucp listen nobody noaccess
3.5 The /etc/cron.d/cron.allow File
The /etc/cron.d/cron.allow file does not exist by default, so all users (except those listed in the cron.deny file) can access their crontab file. By creating a cron.allow file, you can list only those users who can access crontab commands.
The file consists of a list of user names, one per line.
The interaction between the cron.allow and the cron.deny files follows these rules:
- If the cron.allow file exists, only the users listed in this file can create, edit, display, or remove crontab files.
- If the cron.allow file does not exist, all users, except for users listed in the cron.deny file, can create, edit, display, or remove crontab files.
- If neither file exists, only the root user can run the crontab command.
- If a user is listed in both files, the user is denied.