-->
Previous Table of Contents Next


The following example performs its job at 1:23 a.m. If you’re working in the wee hours of the morning before 1:23 a.m. (that is, between midnight and 1:23 a.m.), the command is done today, at 1:23 a.m. Otherwise, it’s done at 1:23 a.m. the following day. The job prints all files in the directory /usr/sales/reports and sends a user named boss some mail announcing that the print job was done at 1:23 a.m. Type the following commands on the terminal, pressing <Return> at the end of each line. After you enter each line, press <Ctrl-d> to finish the command.


at 1:23

lp /usr/sales/reports/*

echo “Files printed, Boss!” | mail -s“Job done” boss


NOTE:  cron jobs, discussed later in this chapter, are the most commonly used mechanisms for running automated system administration jobs under Linux. However, you must be the root user to create and edit cron job entries. The at command allows anyone to run tasks even if he or she does not have root privileges.


See “Setting the Terminal Environment,” p. 341

Commands to be scheduled by at are entered as a list of commands on the line following the at command.

After you terminate the at command, you see a display similar to the following:


job 756603300.a at Tues Jan 21 01:23:00 1997

This response indicates that the job will execute at 1:23 as specified. The job number, 756603300.a, identifies the job. If you decide you want to cancel the job, do so by using the job number associated with it, like so:


at -d 756603300.a

If you have several commands you want to schedule by using at, it’s best to put them in a file. If the filename is getdone, for example, and you want to schedule the commands for 10 a.m., type either


at 10:00 < getdone

or


at 10:00 -f getdone

Remember that the less-than symbol (<) indicates the use of the contents of the getdone file as input to the at command. The -f option allows you to specify the command file without using redirection.

You can also specify a date for an at job. For example, to schedule a job at 5 p.m. on January 24, enter these commands:


at 17:00 Jan 24

lp /usr/sales/reports/*

echo “Files printed, Boss!” | mail -s“Job done” boss

The jobs you schedule with at are put into a queue that the operating system checks periodically. You don’t have to be logged in for the job to be executed. The at command always runs in the background, freeing resources but still accomplishing the job. Any output produced by the commands in your at job is automatically mailed to you.

To see which jobs you scheduled with at, enter at -l. Working with the preceding examples, you see the following results:


job 756603300.a at Sat Dec 21 01:23:00 1996

job 756604200.a at Fri Jan 24 17:00:00 1997

Only your at jobs are listed.

To remove a scheduled at job, enter at -d followed by the job number. To remove the second job just listed, for example, enter this command:


at -d 756604200.a

Table 19.3 summarizes the different ways to use the at command.

Table 19.3 Summary of at Commands

Format Action

at hh:mm Schedules job at the hour (hh) and minute (mm) specified, using a 24-hour clock
at hh:mm month day year Schedules job at the hour (hh), minute (mm), month, day, and year specified
at -l Lists scheduled jobs; an alias for the atq command
at now +count time-units Schedules the job right now plus count number of time-units; time units can be minutes, hours, days, or weeks
at -d job_id Cancels the job with the job number matching job_id; an alias for the atrm command

As the root user, you can use any of these commands; for other users, the files /etc/at.allow and /etc/at.deny determine the permission to use the commands. If /etc/at.allow exists, only the user names listed in the file are allowed to use the at command. If the /etc/at.allow file doesn’t exist, the system checks /etc/at.deny, and every user name not mentioned in /etc/at.deny is allowed to use at (in other words, any user listed in /etc/at.deny isn’t allowed to use at). If neither file exists, only the superuser (root) can use at. If /etc/at.deny is empty, every user can use at.

Running Long Tasks with batch

Linux has more than one command for scheduling tasks. The preceding section describes the at command, which gives you the power to dictate when a task will run. However, it’s always possible that the system can be loaded down with more jobs scheduled at one time than it can comfortably handle. The batch command lets the operating system decide an appropriate time to run a process. When you schedule a job with batch, Linux starts and works on the process whenever the system load isn’t too great. Jobs run under batch execute in the background, just as those run with at. In fact, batch is an alias for at -b in Red Hat Linux.


TIP:  It’s useful to put commands you want to run with at or batch in a file so that you don’t have to retype the commands each time you want to run the jobs. To use batch to schedule the commands in the file getdone, enter the command batch < getdone.

The format for batch commands is to enter the list of commands on the lines following the batch command; you terminate the list of commands with <Ctrl-d>. You can put the list of commands in a file and then redirect the input of the file to batch. To sort a collection of files, print the results, and notify the user named boss that the job is done, enter the following commands:


batch

sort /usr/sales/reports/* | lp

echo “Files printed, Boss!” | mailx -s“Job done” boss

The system returns the following response:


job 7789001234.b at Fri Feb 21 11:43:09 1997


Previous Table of Contents Next