System Administration Guide, Volume II
검색에만이 책은
PDF로 이 문서 다운로드

Scheduling System Events

68

This chapter describes how to schedule routine or one-time system events by using the crontab and at commands. It also explains how to control access to these commands by using cron.deny, cron.allow, and at.deny files.
This is a list of the step-by-step instructions in this chapter.
How to Create or Edit a crontab Filepage 1346
How to Display a crontab Filepage 1348
How to Remove a crontab Filepage 1350
How to Deny crontab Accesspage 1353
How to Limit crontab Access to Specified Userspage 1354
How to Create an at Jobpage 1357
How to Display the at Queuepage 1358
How to Display at Jobspage 1359
How to Remove at Jobspage 1360
How to Deny at Accesspage 1361

Commands for Scheduling System Events

You can schedule system events to execute repetitively, at regular intervals, by using crontab. Or, you can schedule a single system event for execution at a specified time by using at. Table 68-1 summarizes crontab and at, as well as the files that enable you to control access to these commands.
Table 68-1
CommandWhat It SchedulesLocation of FilesFiles That Control Access
crontabRepetitive system events/usr/spool/cron/crontabs
or /var/spool/cron/crontabs
/etc/cron.d/cron.allow
and /etc/cron.d/cron.deny
atA single system event/usr/spool/cron/atjobs
or /var/spool/cron/atjobs
/etc/cron.d/at.deny

Scheduling a Repetitive System Event

The following sections describe how to create, edit, display, and remove crontab files, as well as how to control access to them.

Syntax of crontab File Entries

A crontab file consists of commands, one per line, that execute automatically at the time specified by the first five fields at the beginning of each command line. These first five fields, described in Table 68-2, are separated by spaces. They indicate when the command will be executed.
Table 68-2 crontab
Time FieldValues
Minute0-59
Hour0-23
Day of month1-31
Month1-12
Day of week0-6 (0=Sunday)
Follow these guidelines to use special characters in crontab time fields:
  • Use a space to separate each field.
  • Use a comma to separate multiple values.
  • Use a hyphen to designate a range of values.
  • Use an asterisk as a wildcard to include all possible values.
  • Use a comment mark (#) at the beginning of a line to indicate a comment or a blank line.
For example, the following sample crontab command entry displays a reminder in the user's console window at 4 p.m. on the first and fifteenth of every month.

  16 0 1,15 * * echo Timesheets Due > /dev/console  

Each command within a crontab file must consist of one line, even if it is very long, because crontab does not recognize extra carriage returns. For more detailed information about crontab entries and command options, refer to the crontab(1)man page.

Creating and Editing crontab Files

The simplest way to create a crontab file is to use the crontab -e command to invoke the text editor set up for your system environment, defined by the EDITOR environment variable. If this variable has not been set, crontab uses the default editor ed.
Unless you have set up an editor for your account, the crontab facility defaults to ed. Define your EDITOR environment to be an editor you are familiar with. The following example shows how to check to see whether an editor has been defined, and how to set up vi as the default.

  $ which $EDITOR  
  EDITOR: Undefined variable  
  $ setenv EDITOR /usr/bin/vi  

When you create a crontab file, it is automatically placed in the /usr/spool/cron/crontabs directory and is given your user name. You can create or edit a crontab file for another user, or root, if you have root privileges.
Enter crontab command entries as described in "Syntax of crontab File Entries" on page 1344.

· How to Create or Edit a crontab File

  1. Be sure that you have access to the editor of your choice.

  2. (Optional) To create or edit a crontab file belonging to root or another user, become root.

  3. Create a new crontab file, or edit an existing one.


  $ crontab -e [username]  

In this command,
username.......Is the name of another user's account, and requires root privileges to create or edit.

CAUTION Caution - If you accidentally enter the crontab command with no option, press the interrupt character for your editor. This allows you to quit without saving changes. Exiting the file and saving changes at this point would overwrite an existing crontab file with an empty file.

  1. Add command lines to the file, following the syntax described in "Syntax of crontab File Entries" on page 1344.

  2. Exit the file, saving the changes.

    The crontab file will be placed in /usr/spool/cron/crontabs.

Verification--Creating or Editing a crontab File

To verify that a crontab file exists for a user, use the ls -l command in the /usr/spool/cron/crontabs directory. For example, the following display shows that crontab files exist for users smith and jones.

  $ ls -l /usr/spool/cron/crontabs  
  -rw-r--r--  1 root     sys          190 Feb 26 16:23 adm  
  -rw-------  1 root     staff        225 Mar  1  9:19 jones  
  -rw-r--r--  1 root     root        1063 Feb 26 16:23 lp  
  -rw-r--r--  1 root     sys          441 Feb 26 16:25 root  
  -rw-------  1 root     staff         60 Mar  1  9:15 smith  
  -rw-r--r--  1 root     sys          308 Feb 26 16:23 sys  

Verify the contents of user's crontab file by using crontab -l as described in "How to Display a crontab File" on page 1348.

Example--Creating or Editing a crontab File

The following example shows how to create a crontab file for another user.

  $ su  
  Password:  
  # crontab -e jones  

The following command entry added to a new crontab file will automatically remove any log files from the user's home directory at 1 a.m. every Sunday. Because the command entry does not redirect output, redirect characters are added to the command line after *.log to make sure that the command executes properly.

  # This command helps clean up user accounts.  
  1 0 * * 6 rm /home/jones/*.log > /dev/null 2>&1  

Displaying crontab Files

The crontab -l command displays the contents of your crontab file much the way the cat command displays the contents of other types of files. You do not have to change directories to /usr/spool/cron/crontabs (where crontab files are located) to use this command.
By default, the crontab -l command displays your own crontab file. To display crontab files belonging to other users, you must be root.

· How to Display a crontab File

  1. (Optional) To display a crontab file belonging to root or another user, become root.

  2. Display the crontab file.


  $ crontab -l [username]  

In this command, username is another user's account, and requires root privileges to display.
username.......Is the name of another user's account, and requires root privileges to create or edit.

CAUTION Caution - If you accidentally enter the crontab command with no option, press the interrupt character for your editor. This allows you to quit without saving changes. Exiting the file and saving changes at this point would overwrite an existing crontab file with an empty file.

Example--Displaying a crontab File

The following example shows how to use crontab -l to display the contents of the default user's crontab file, the default root crontab file, and the crontab file belonging to another user.

  $ crontab -l  
  13 13 * * * chmod g+w /usr/documents/*.book > /dev/null 2>&1  
  $ su  
  Password:  
  # crontab -l  
  #ident "@(#)root    1.12    94/03/24 SMI"    /* SVr4.0 1.1.3.1    */  
  #  
  # The root crontab should be used to perform accounting data  
  # collection.  
  #  
  # The rtc command is run to adjust the real time clock if and when  
  # daylight savings time changes.  
  #  
  0 2 * * 0,4 /etc/cron.d/logchecker  
  5 4 * * 6   /usr/lib/newsyslog  
  15 3 * * * /usr/lib/fs/nfs/nfsfind  
  1 2 * * * [ -x /usr/sbin/rtc ] && /usr/sbin/rtc -c > /dev/null 2>&1  
  0 1 * * * /usr/sbin/cfsadmin -s all  
  # crontab -l jones  
  13 13 * * * cp /home/jones/work_files /usr/backup/. > /dev/null  
  2>&1  

Removing crontab Files

By default, crontab file protections are set up so that you cannot inadvertently delete a crontab file by using the rm command. Instead, use the crontab -r command to remove crontab files.
By default, crontab -r removes your own crontab file. You must be root to remove crontab files belonging to root or other users.
You do not have to change directories to /usr/spool/cron/crontabs (where crontab files are located) to use this command.

· How to Remove a crontab File

  1. (Optional) To remove a crontab file belonging to root or another user, become root.

  2. Remove the crontab file.


  $ crontab -r [username]  

In this command,
username.......Is the name of another user's account, and requires root privilegs to create or edit.

CAUTION Caution - If you accidentally enter the crontab command with no option, press the interrupt character for your editor. This allows you to quit without saving changes. Exiting the file and saving changes at this point would overwrite an existing crontab file with an empty file.

Verification--Removing a crontab File

You can verify that you have removed a crontab file by using the ls command within the /usr/spool/cron/crontabs directory to display the existing crontab files.
To verify that you have removed a crontab file, use the ls command to check the crontab directory.

  # ls /usr/spool/cron/crontabs  
  adm    jones    lp     root    sys  
  # crontab -r jones  
  # ls /usr/spool/cron/crontabs  
  adm     lp     root    sys  

Example--Removing a crontab File

The following example shows how to use crontab -r to remove the default user's crontab file, as well as crontab files belonging to root and another user. ls verifies that the correct crontab files have been removed.

  $ ls /usr/spool/cron/crontabs  
  adm     jones     lp     root    smith    sys  
  $ crontab -r  
  $ ls /usr/spool/cron/crontabs  
  adm     jones     lp     root    sys  
  $ su  
  Password:  
  # crontab -r  
  # ls /usr/spool/cron/crontabs  
  adm     jones     lp    sys  
  # crontab -r jones  
  # ls /usr/spool/cron/crontabs  
  adm     lp    sys  

Controlling Access to crontab

You can control access to crontab by using two files in the /etc/cron.d directory: cron.deny and cron.allow. These files permit only specified users to perform crontab tasks such as creating, editing, displaying, or removing their own crontab files.
The cron.deny and cron.allow files consist of a list of user names, one per line. These access control files work together like this:
  • If cron.allow exists, only the users listed in this file can create, edit, display, or remove crontab files.
  • If cron.allow doesn't exist, all users may submit crontab files, except for users listed in cron.deny.
  • If neither cron.allow nor cron.deny exists, only root can run crontab.
During Solaris software installation, a default cron.deny file is provided:

  $ cat /etc/cron.d/cron.deny  
  daemon  
  bin  
  smtp  
  nuucp  
  listen  
  nobody  
  noaccess  

No default cron.allow file is supplied. This means that, after Solaris software installation, all users (except the ones listed in the default cron.deny file) can access crontab.

· How to Deny crontab Access

  1. Become root.

  2. Using the editor of your choice, edit the /etc/cron.d/cron.deny file to add user names, one per line, who will be prevented from using crontab commands.


  daemon  
  bin  
  smtp  
  nuucp  
  listen  
  nobody  
  noaccess  
  username1  
  username2  
  username3  
  .  
  .  
  .  

  1. Exit the file, saving the changes.

· How to Limit crontab Access to Specified Users

  1. Become root.

  2. Use the editor of your choice to create a file named

/etc/cron.d/cron.allow.

  1. Enter the user names, one per line, who will be allowed to use crontab commands.


  root  
  username1  
  username2  
  username3  
  .  
  .  
  .  

Be sure to add root to this list. If you do not, root access to crontab commands will be denied.
  1. Exit the file, saving the changes.

Verification--Limiting crontab Access to Specified Users

To verify whether or not a specific user can access crontab, use the crontab -l command while logged into the user account.

  $ crontab -l  

If the user can access crontab, and already has created a crontab file, it will be displayed. Otherwise, if the user can access crontab but no crontab file exists, a message like the following will be displayed:

  crontab: can't open your crontab file  

This user is either listed in cron.allow (if it exists), or is not listed in cron.deny.
If the user cannot access crontab, the following message is displayed whether or not a previous crontab file exists:

  crontab: you are not authorized to use cron. Sorry.  

This means either that the user is not listed in cron.allow (if it exists), or the user is listed in cron.deny.

Examples--Limiting crontab Access to Specified Users

The following example shows a cron.deny file that prevents user names visitor, jones, and temp from accessing crontab.

  $ cat /etc/cron.d/cron.deny  
  daemon  
  bin  
  smtp  
  nuucp  
  listen  
  nobody  
  noaccess  
  jones  
  temp  
  visitor  

The following example shows a cron.allow file. The users smith, jones, lp, and root are the only ones who may access crontab.

  $ cat /etc/cron.d/cron.allow  
  root  
  jones  
  lp  
  smith  

Scheduling a Single System Event

The following sections describe how to use at to schedule jobs, such as commands and scripts, for execution at a later time, as well as how to display and remove these jobs, and how to control access to at.
By default, users can create, display, and remove their own at job files. To access at files belonging to root or other users, you must have root privileges.
When you submit an at job, it is assigned a job identification number along with the .a extension that becomes its file name.

at Command Description

Submitting an at job file includes:
  1. Invoking the at utility, specifying a command execution time.

  2. Entering a command or script to execute later. If output from this command or script is important, be sure to direct it to a file for later examination.

For example, the following at job removes core files from the user account belonging to Smith near midnight on the last day of March.

  $ at 11:45pm mar 31  
  at> rm /home/smith/*core*  
  at> Press Control-d  
  job 793924770.a at Fri Mar 31 23:45:00 1995  

· How to Create an at Job

  1. Enter the at facility, specifying the time you want your job executed, and press Return.


  $ at [-m] time [date]  

In this command,
-mSends you mail after the job is completed.
timeIs the hour that you want to schedule the job. Add am or pm if you do not specify the hours according to a 24-hour clock. midnight, noon, and now are acceptable keywords. Minutes are optional.
dateIs the first three or more letters of a month, a day of the week, or the keywords today or tomorrow.
  1. At the at prompt, enter the commands or scripts you want to execute, one per line. You may enter more than one command by pressing Return at the end of each line.

  2. Exit the at utility and save the at job by pressing Control-d. Your at job is assigned a queue number, which is also its file name. This number is displayed when you exit the at utility.

Verification--Creating an at Job

To verify that you have created an at job, use the atq command (described in "How to Display at Jobs" on page 1359). atq confirms that at jobs belonging to jones have been submitted to the queue.

  $ atq  
  Rank     Execution Date     Owner      Job         Queue   Job Name  
   1st   Feb 28, 1995 14:30   jones    793920600.a     a     stdin  
   2nd   Feb 29, 1995 08:10   jones    793962720.a     a     stdin  

Examples--Creating an at Job

The following example shows the at job that user jones created to remove her backup files at 7:30 p.m. She used the -m option so that she would receive a mail message after her job completed.

  $ at -m 1930  
  at> rm /home/jones/*.backup  
  at> Press Control-d  
  job 543962000.a at Tue Feb 28 19:30:00 1995  

She received a mail message which confirmed the execution of her at job.

  Your "at" job "rm /home/jones/*.backup" completed.  

The following example shows how Jones scheduled a large at job for 4:00 Saturday morning.

  $ at 4 am Saturday  
  at> sort -r /usr/dict/words > /export/home/jones/big.file  

· How to Display the at Queue

To check your jobs that are waiting in the at queue, use the atq command. This command displays status information about the at jobs that you created.

  $ atq  

Example--Displaying the at Queue

The following example shows output from the atq command on February 27, 1995. Execution times and dates for three jobs submitted by jones are displayed.

  $ atq  
  Rank     Execution Date     Owner      Job         Queue   Job Name  
   1st   Feb 28, 1995 14:30   jones    793920600.a     a     stdin  
   2nd   Mar  1, 1995 08:10   jones    793962720.a     a     stdin  
   3rd   Mar  1, 1995 12:00   jones    793991450.a     a     stdin  

· How to Display at Jobs

To display information about the execution times of your at jobs, use the at -l command.

  $ at -l [job-id]  

In this command,
job-id..........Is the identification number of the job whose status you want to examine.

Example--Displaying at Jobs

The following example shows output from the at -l command, used to get status information on all jobs submitted by a user.

  $ at -l  
  793920600.a     Tue Feb 28 14:30:00 1995  
  793962720.a     Wed Mar 01 08:10:00 1995  
  793991450.a     Wed Mar 01 12:00:00 1995  

The following example shows output displayed when a single job is specified with the at -l command.

  $ at -l 793962720.a  
  793962720.a     Wed Mar 01 08:10:00 1995  

· How to Remove at Jobs

  1. (Optional) To remove an at job belonging to root or another user, become root.

  2. Remove the at job from the queue before it is executed.


  $ at -r [job-id]  

In this command,
job-id........Is the identification number of the job you want to remove.

Verification--Removing at Jobs

To verify that you have removed an at job, use the at -l (or the atq) command to display the jobs remaining in the at queue. The job whose identification number you specified should not appear.

Example--Removing at Jobs

In the following example, a user wants to remove an at job that was scheduled to execute at noon on March 1. First, the user displays the at queue to locate the job identification number. Next, the user removes this job from the at queue. Finally, the user displays the at queue again to confirm that this job has been removed.

  $ at -l  
  793920600.a     Tue Feb 28 14:30:00 1995  
  793962720.a     Wed Mar 01 08:10:00 1995  
  793991450.a     Wed Mar 01 12:00:00 1995  
  $ at -r 793991450.a  
  $ at -l 793991450.a  
  at: 793991450.a does not exist  

Controlling Access to at

Users listed in the at.deny file cannot use at to schedule jobs or to check the at queue status.
The at.deny file is placed in the /etc/cron.d directory during Solaris software installation. At that time, the same users are listed in both this file and the default cron.deny file.

  daemon  
  bin  
  smtp  
  nuucp  
  listen  
  nobody  
  noaccess  

Root permissions are required to edit this file.

· How to Deny at Access

  1. Become root.

  2. Using the editor of your choice, open the /etc/cron.d/at.deny file.

  1. Add or remove the names of users, one per line, who will be prevented from using at commands.


  daemon  
  bin  
  smtp  
  nuucp  
  listen  
  nobody  
  noaccess  
  username1  
  username2  
  username3  
  .  
  .  
  .  

  1. Exit the file, saving your changes.

Verification--Denying at Access

To verify whether or not a user's name was added correctly to /etc/cron.d/at.deny, use the at -l command while logged in as the user. If the user cannot access at commands, the following message is displayed.

  # su smith  
  Password:  
  $ at -l  
  at: you are not authorized to use at.  Sorry.  

Likewise, if the user tries to submit an at job, the following message is displayed:

  $ at 2:30pm  
  at: you are not authorized to use at.  Sorry.  

This confirms that the user is listed in the at.deny file.

Example--Denying at Access

The following example shows an at.deny file that has been edited so that the users Smith and Jones may not access at.

  $ cat at.deny  
  daemon  
  bin  
  smtp  
  nuucp  
  listen  
  nobody  
  noaccess  
  jones  
  smith  

The following example shows that if a user is not listed in at.deny, he can access at, display the at prompt, and receive output confirming that his at job has been accepted and assigned a number in the at queue. The command atq confirms this.

  $ at 4:55pm  
  at> echo Testing > /dev/console  
  at> Press Control-d  
  warning: commands will be executed using /bin/csh  
  job 843720330.a at Thu Feb 23 16:55:00 1995  
  $ atq  
  Rank     Execution Date     Owner      Job         Queue   Job Name  
   1st   Feb 23, 1995 16:55   smith    843720330.a      a     stdin  

This means that smith is not listed in the at.deny file.