Process Scheduling


As a time-sharing system, the UNIX system kernel directly controls how processes are scheduled. User-level commands are available that allow you to specify when you would like processes to be run.

The at Command

You can specify when commands should be executed by using the at command, which reads commands from its standard input and schedules them for execution. Normally, standard output and standard error (see Chapter 4) are mailed to you, unless you redirect them elsewhere. UNIX accepts several ways of indicating the time; consult the manual page at(1) for all the alternatives. Here are some examples of alternative time and date formats:

 at 0500 Jan 18 at 5:00 Jan 18 at noon at 5 pm tomorrow

The at command is handy for sending yourself reminders of important scheduled events. For example, the command

 $ at 6 am Friday echo "Don't Forget The Meeting with BOB at 1 PM!!" mail you CTRL-D

will mail you the reminder early Friday morning. at continues to read from standard input until you terminate input with CTRL-D.

You can redirect standard output back to your terminal and use at to interrupt with a reminder. Use the ps command just discussed to find out your terminal number. Include this terminal number in a command line such as this:

 $ at 1 pm today echo "^G^GConference call with BOB at 1 PM^G^G" > /dev/term/43 CTRL-D

This will display the following message on your screen at 1:00 P.M.

 Conference call with BOB at 1 PM

The ^G (CTRL-G) characters in the echo command will ring the bell on the display terminal. Because at would normally mail you the output of banner and echo, you have to redirect them to your terminal if you want them to appear on the screen.

The -f option to at allows you to run a sequence of commands contained in a file. The command

 $ at -f scriptfile 6 am Monday

will run scriptfile at 6 A.M. on Monday If you include a line similar to

 at -f scriptfile 6 am tomorrow

at the end of scriptfile, the script will be run every morning. You can learn how to write shell daemons in Chapter 20.

If you want to see a listing of all the at jobs you have scheduled, use the command

 $ at -1 629377200.a      Sun Aug 06 06:00:00 2006

With the -l option, at returns the ID number and scheduled time for each of your at jobs. To remove a job from the queue, use the -r option. The command

 at -r 629377200.a

will delete the job scheduled to run at 6 A.M. on August 6, 2006. Notice that the time and date are the only meaningful information provided. To make use of this listing, you need to remember which commands you have scheduled at which times.

The at command is used most effectively when you want to schedule a process that either is not part of a normal routine or is an event you wish to perform only once. While you can use the at command to schedule processes that run routinely, you should use the cron facility (discussed later in this chapter) to do this. The cron command provides more robust management of the process that is requested to be run, and-once set up properly-will run without intervention time after time.

The cron Facility

The cron facility is a system daemon that executes commands at specific times. It is similar in some respects to the at command (previously discussed in this chapter) but is much more useful for repetitive execution of a process. The command and schedule information are kept in the directory /var/spool/cron/crontabs or in /usr/spool/cron/crontabs. Each user who is entitled to directly schedule commands with cron has a crontab file. cron wakes up periodically (usually once each minute) and executes any jobs that are scheduled for that minute.

Entries in a crontab file have six fields, as shown in the following example.

The first field is the minute that the command is to run; the second field is the hour; the third, the day of the month; the fourth, the month of the year; the fifth, the day of the week; and the sixth is the command string to be executed. Asterisks act as wildcards. In the crontab example, the program with the pathname /home/gather is executed and mailed to “maryf” every day at 6 P.M. The program /home/jar/bin/backup is executed every day at 2:30 A.M.

 # # # MIN HOUR DOM MOY DOW COMMAND # #(0–59)  (0–23)  (1–31)  (1–12)  (0–6) (Note: 0=Sun) #______  ______  ______  ______  _____ _____________ # 0        18      *       *       *      /home/gather | mail maryf 30       2       *       *       *      /home/jar/bin/backup

The file /etc/cron.d/cron.allow contains the list of all users who are allowed to make entries in the crontab. If you are a system administrator, or if this is your own system, you will be able to modify the crontab files. If you are not allowed to modify a crontab file, use the at command to schedule your jobs (unless you are denied access to at jobs via the at.deny file).

The crontab Command

To make an addition in your crontab file, you use the crontab command. For example, you can schedule the removal of old files in your wastebasket with an entry like this:

 0 1    * * 0 cd /home/jar/.wastebasket; find . -atime +7 -exec /bin/rm -r {} 2> /dev/null ;

This entry says, “Each Sunday at 1 A.M., go to jar’s wastebasket directory and delete all files that are more than 7 days old.” If you place this line in a file named wasterm, and issue the command

 $ crontab wasterm

the line will be placed in your crontab file. If you use crontab without specifying a file, the standard input will be placed in the crontab file. The command

 $ crontab CTRL-D

deletes the contents of your crontab-that is, it replaces the contents with nothing. This is a common error and causes the contents of crontab to be deleted by mistake.

Note that the scheduling of processing depends on the accuracy of the system time. If unpredictable things start happening, you might want to check that this time is accurate.

The batch Command

The batch command lets you defer the execution of a command but does not give you control over when it is run. The batch command is especially handy when you have a command or set of commands to run but don’t care exactly when they are executed. batch will queue the commands to be executed when the load on the system permits. Standard output and standard error are mailed to you unless they are redirected. The here document construct supported by the shell (discussed in Chapter 20) can also be used to provide input to batch.

 $ batch <<! cat mybook tbl | eqn troff -mm lp !

Daemons

Daemons are processes that are not connected to a display; they may run in the background, and they do useful work. Several daemons are normally found on UNIX Systems: user daemons, like the one described in Chapter 20, that clean up your files; and system daemons that handle scheduling and administration. There are also a number of system daemons that are started automatically when the system boots up to set up your network environment and all of the services available to you.

For example, /init.d/ is a standard directory that is used to contain daemon scripts that control user states, mail environments, and even file system choices. Similar daemons handle printing and the operation of the printer spool, file backup, cleanup of temporary directories, and billing operations. Each of these daemons is controlled by cron, which is itself run by init (PID 1).

You can create daemons from processes so that they will start automatically. One way is to consider all the tasks that are affected, the options required, and the security level needed, and then create the daemon process. A simpler way is to use daemon, which is supported on all of the major UNIX/LINUX platforms. daemon can be obtained at http://libslack.org/daemon/. It is freeware available under the GNU General Public License.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net