Содержащиеся вНайти другие документыРесурсы поддержки | Загрузить это руководство в формате PDF (1611 КБ)
Chapter 30 Scheduling System Events (Tasks)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. Commands for Scheduling System EventsYou can schedule system events to execute repetitively, at regular intervals, by using the crontab command.You can schedule a single system event for execution at a specified time by using the at command. The following table summarizes crontab and at, as well as the files that enable you to control access to these commands. Table 30-1 Command Summary: Scheduling System Events
Scheduling a Repetitive System Event (cron)The following sections describe how to create, edit, display, and remove crontab files, as well as how to control access to them. Inside a crontab FileThe cron daemon schedules system events according to commands found within each crontab file. A crontab file consists of commands, one per line, that will be executed at regular intervals. The beginning of each line contains date and time information that tells the cron daemon when to execute the command. For example, a crontab file named root is supplied during SunOS software installation. Its contents include these command lines:
The first command line instructs the system to run logchecker at 3:10 on Sundays and Thursdays nights. The second command line schedules the system to run newsyslog at 3:10 every Sunday morning. The third command line orders the system to execute nfsfind Sundays at 3:15 in the morning. The fourth command line instructs the system to check daily for daylight savings time and make corrections if necessary. If there is no RTC time zone nor an /etc/rtc_config file, this entry will do nothing. The fifth command line instructs the system to check for and remove duplicate entries in the Generic Security Service table, /etc/gss/gsscred_db. For more information about the syntax of lines within a crontab file, see "Syntax of crontab File Entries". The crontab files are stored in /var/spool/cron/crontabs. Several crontab files besides root are provided during SunOS software installation (see the following table). Table 30-2 Default crontab Files
Besides the default crontab file, users can create crontab files to schedule their own system events. Other crontab files are named after the user accounts in which they are created, such as bob, mary, smith, or jones. To access crontab files belonging to root or other users, superuser privileges are required. Procedures explaining how to create, edit, display, and remove crontab files are described in "Commands for Scheduling System Events". How the cron Daemon Handles SchedulingThe cron daemon handles the automatic scheduling of crontab commands. Its function is to check the /var/spool/cron/crontab directory for the presence of crontab files, normally every 15 minutes. It checks for new crontab files or changes to existing ones, reads the execution times listed within the files, and submits the commands for execution at the proper times. In much the same way, the cron daemon controls the scheduling of at files, which are stored in the /var/spool/cron/atjobs directory. Syntax of crontab File EntriesA 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 the following table, are separated by spaces. They indicate when the command will be executed. Table 30-3 Values for crontab Time Fields
Follow these guidelines to use special characters in crontab time fields:
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.
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 crontab(1). Creating and Editing crontab FilesThe 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. 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.
When you create a crontab file, it is automatically placed in the /var/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 superuser privileges. Enter crontab command entries as described in "Syntax of crontab File Entries". How to Create or Edit a crontab File
Example--Creating or Editing a crontab FileThe following example shows how to create a crontab file for another user.
The following command entry added to a new crontab file will automatically remove any log files from the user's home directory at 1:00 am every Sunday morning. 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.
How to Verify a crontab FileTo verify that a crontab file exists for a user, use the ls -l command in the /var/spool/cron/crontabs directory. For example, the following display shows that crontab files exist for users smith and jones.
Verify the contents of user's crontab file by using crontab -l as described in "How to Display a crontab File". Displaying crontab FilesThe 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 /var/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 superuser. How to Display a crontab File
Example--Displaying a crontab FileThe 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.
Removing crontab FilesBy 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 superuser to remove crontab files belonging to superuser or other users. You do not have to change directories to /var/spool/cron/crontabs (where crontab files are located) to use this command. How to Remove a crontab File
Example--Removing a crontab FileThe following example shows how user smith uses the crontab -r command to remove his crontab file.
Controlling Access to crontabYou 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:
Superuser privileges are required to edit or create the cron.deny and cron.allow files. During SunOS software installation, a default cron.deny file is provided:
None of these user names can access crontab commands. You can edit this file to add other user names who will be denied access to the crontab command. 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. If you create a cron.allow file, only these users can access crontab commands. How to Deny crontab Access
How to Limit crontab Access to Specified Users
Examples--Limiting crontab Access to Specified UsersThe following example shows a cron.deny file that prevents user names visitor, jones, and temp from accessing crontab.
The following example shows a cron.allow file. The users smith, jones, lp, and root are the only ones who may access crontab.
How to Verify Limited crontab AccesssTo verify whether or not a specific user can access crontab, use the crontab -l command while logged into the user account.
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:
This user either is 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:
This means either that the user is not listed in cron.allow (if it exists), or the user is listed in cron.deny. Scheduling a Single System Event (at)The following sections describe how to use at(1) to schedule jobs (commands and scripts) for execution at a later time, how to display and remove these jobs, and how to control access to the at command. 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 superuser 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 DescriptionSubmitting an at job file includes:
For example, the following at job removes core files from the user account smith near midnight on the last day of July.
at Command SecurityYou can set up a file to control access to the at command, permitting only specified users to create, remove, or display queue information about their at jobs. The file that controls access to at, /etc/cron.d/at.deny, consists of a list of user names, one per line. The users listed in this file cannot access at commands. The at.deny file, created during SunOS software installation, contains the following user names:
With superuser privileges, you can edit this file to add other user names whose at access you want to restrict. How to Create an at Job
Examples--Creating an at JobThe following example shows the at job that user jones created to remove her backup files at 7:30 at night. She used the -m option so that she would receive a mail message after her job completed.
She received a mail message which confirmed the execution of her at job.
The following example shows how jones scheduled a large at job for 4:00 Saturday morning. The output of which was directed to big.file.
How to Display the at QueueTo 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.
How to Verify an at JobTo verify that you have created an at job, use the atq command. The atq command confirms that at jobs belonging to jones have been submitted to the queue.
How to Display at JobsTo display information about the execution times of your at jobs, use the at -l command.
Example--Displaying at JobsThe following example shows output from the at -l command, used to get status information on all jobs submitted by a user.
The following example shows output displayed when a single job is specified with the at -l command.
How to Remove at Jobs
Example--Removing at JobsIn the following example, a user wants to remove an at job that was scheduled to execute at 4 am on July 17th. 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 verifies that this job has been removed from the queue.
Controlling Access to atUsers 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.
Root permissions are required to edit this file. How to Deny at Access
Example--Denying at AccessThe following example shows an at.deny file that has been edited so that the users smith and jones may not access the at command.
How to Verify at Access Is DeniedTo 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.
Likewise, if the user tries to submit an at job, the following message is displayed:
This confirms that the user is listed in the at.deny file. If at access is allowed, the at -l command returns nothing. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||