Inom
Hitta mer dokumentationSupportresurser som ingår | Ladda ner denna bok i PDF (1930 KB)
Chapter 18 Scheduling System Tasks (Tasks)This chapter describes how to schedule routine or single (one-time) systems tasks by using the crontab and at commands. This chapter 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. Ways to Automatically Execute System TasksYou can set up many system tasks to execute automatically. Some of these tasks should occur at regular intervals. Other tasks need to run only once, perhaps during off hours such as evenings or weekends. This section contains overview information about two commands, crontab and at, which enable you to schedule routine tasks to execute automatically. The crontab schedules repetitive commands. The at command schedules tasks that execute once. The following table summarizes crontab and at commands, as well as the files that enable you to control access to these commands. Table 18–1 Command Summary: Scheduling System Tasks
You can also use the Solaris Management Console's Scheduled Jobs tool to schedule routine tasks. For information on using and starting the Solaris Management Console, see “Working With the Management Console (Tasks)” in System Administration Guide: Basic Administration. For Scheduling Repetitive Jobs: crontabYou can schedule routine system administration tasks to execute daily, weekly, or monthly by using the crontab command. Daily crontab system administration tasks might include:
Weekly crontab system administration tasks might include:
Monthly crontab system administration tasks might include:
Additionally, users can schedule crontab commands to execute other routine system tasks, such as sending reminders and removing backup files. For step-by-step instructions on scheduling crontab jobs, see How to Create or Edit a crontab File. For Scheduling a Single Job: atThe at command allows you to schedule a job for execution at a later time. The job can consist of a single command or a script. Similar to crontab, the at command allows you to schedule the automatic execution of routine tasks. However, unlike crontab files, at files execute their tasks once, and then are removed from their directory. Therefore, at is most useful for running simple commands or scripts that direct output into separate files for later examination. Submitting an at job involves entering a command and following the at command syntax to specify options to schedule the time your job will be executed. For more information about submitting at jobs, see Description of the at Command. The at command stores the command or script you entered, along with a copy of your current environment variable in the /var/spool/cron/atjobs directory. Your at job file name is given a long number that specifies its location in the at queue, followed by the .a extension, such as 793962000.a. The cron daemon periodically executes the atrun program, usually at 15-minute intervals. Then the atrun program executes at jobs at their scheduled times. After the cron daemon executes your at job, the at job's file is removed from the atjobs directory. For step-by-step instructions on scheduling at jobs, see How to Create an at Job. Scheduling a Repetitive System Task (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 tasks 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. The file's contents include these command lines:
The first line runs the logadm command at 3:10 a.m. every day. The second line executes the nfsfind script every Sunday at 3:15 a.m. The third line runs a script that checks for daylight savings time (and make corrections if necessary) at 2:10 a.m. daily. If there is no RTC time zone nor an /etc/rtc_config file, this entry does nothing. The fourth line checks for (and removes) duplicate entries in the Generic Security Service table, /etc/gss/gsscred_db, at 3:30 a.m. daily. For more information about the syntax of lines within a crontab file, see Syntax of crontab File Entries. The crontab files are stored in the /var/spool/cron/crontabs directory. Several crontab files besides root are provided during SunOS software installation (see the following table). Table 18–2 Default crontab Files
Besides the default crontab files, users can create crontab files to schedule their own system tasks. 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 that belong to root or other users, superuser privileges are required. Procedures explaining how to create, edit, display, and remove crontab files are described in subsequent sections. How the cron Daemon Handles SchedulingThe cron daemon manages the automatic scheduling of crontab commands. The role of the cron daemon is to check the /var/spool/cron/crontab directory for the presence of crontab files, normally every 15 minutes. The cron daemon 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. Table 18–3 Acceptable Values for crontab Time Fields
Follow these guidelines for using special characters in crontab time fields:
For example, the following 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 that line 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. This command invoke the text editor set up for your system environment, which is defined by the EDITOR environment variable. If this variable has not been set, crontab uses the default editor, ed. Preferably, you should choose an editor that you know well. The following example shows how to determine if 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. 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 automatically removes any log files from the user's home directory at 1:00 a.m. 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 the crontab -l command 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 that belong to other users, you must be superuser. How to Display a crontab File
Example—Displaying a crontab FileThe following example shows how to use the crontab -l command to display the contents of the user's default 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 that belong 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 the crontab CommandYou can control access to the crontab command by using two files in the /etc/cron.d directory: cron.deny and cron.allow. These files permit only specified users to perform the crontab command 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 as follows:
Superuser privileges are required to edit or create the cron.deny and cron.allow files. The cron.deny file, created during SunOS software installation, contains the following user names:
None of the user names in the default cron.deny file can access the crontab command. You can edit this file to add other user names that will be denied access to the crontab command. No default cron.allow file is supplied. So, after Solaris software installation, all users (except the ones listed in the default cron.deny file) can access the crontab command. If you create a cron.allow file, only these users can access the crontab command. 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 the crontab command.
The following example shows a cron.allow file. The users smith, jones, lp, and root are the only ones who can access the crontab command.
How to Verify Limited crontab AccessTo verify if a specific user can access crontab, use the crontab -l command while you are logged into the user account.
If the user can access crontab, and already has created a crontab file, the file is displayed. Otherwise, if the user can access crontab but no crontab file exists, a message such as the following is displayed:
This user either is listed in cron.allow (if the file exists), or the user is not listed in cron.deny. If the user cannot access the crontab command, the following message is displayed whether or not a previous crontab file exists:
This message means that either the user is not listed in cron.allow (if the file exists), or the user is listed in cron.deny. Scheduling a Single System Task (at)The following sections describe how to use the at command 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 that belong 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, which becomes the job's file name. Description of the at CommandSubmitting an at job file involves:
For example, the following at job removes core files from the user account smith near midnight on the last day of July.
Controlling access to the at CommandYou 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 the at command, /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 the at.deny 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 p.m. She used the -m option so that she would receive an email 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 a.m. Saturday morning. The job output 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 that belong 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.
Where the -l job-id option identifies the identification number of the job whose status you want to display. Example—Displaying at JobsThe following example shows output from the at -l command, which provides status information on all jobs submitted by a user.
The following example shows the 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 a.m. 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.
How to Deny Access to the at Command
Example—Denying at AccessThe following example shows an at.deny file that has been edited so that the users smith and jones cannot access the at command.
How to Verify that at Access Is DeniedTo verify that a username 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 the at command, the following message is displayed.
Likewise, if the user tries to submit an at job, the following message is displayed:
This message confirms that the user is listed in the at.deny file. If at access is allowed, the at -l command returns nothing. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||