Main index

Introducing UNIX and Linux


Processes and devices

Overview
Processes
      Process status
      Foreground and background
      Process control
      Signals
Environment
      Environment variables
      Global and local variables
      Executable scripts
Program control
      Job control
      Command history list
      Running a job at a specific time
      Running programs periodically
      Big programs
      Timing a program
      Running programs in order
Quotes and escapes
Devices
Backquotes
Summary
Exercises

Running a job at a specific time

During a session on your UNIX system, most of the programs you run will be executed immediately. Sometimes this will not be desirable. If a program is likely to take a long time to run, you may wish it to run overnight since, if a machine tries to execute too many processes at once, it becomes slow, to the detriment of all logged-in users. The command at can be used to schedule a job for a specific time in the future.

In its simplest form, typing at followed by a time and/or date will cause the standard input to be read, and those commands entered as input to be executed at that time, thus:

$ at 1530
at> echo "It's half past three"

ctrl-D
job 81 at 2001-12-07 15:30

The number of the job, together with confirmation of the date and time it has been scheduled, will be printed on the terminal. The output (both standard output and standard error) will be mailed to you. Alternatively, you can create a file (testfile, say) containing commands you wish to be executed, and then you can use at with option -f ('file'):

at -f testfile 1530
job 81 at 2001-12-07 15:30

The formats allowed for you to specify the time are broad and easy to use, but rather complex to specify - look in the manual page for at to check the exact syntax allowed. The following examples will give the general idea:

1645
16:45
16:45 GMT tomorrow
noon
4am Jan 25 2001
11pm today
now + 30 minutes
now + 1 month

If you take care that your time/date specification is unambiguous, it will probably be acceptable to at.

When a job has been created using at, it is placed on a queue. At the specified time and date, or as soon thereafter as the load on the system permits, an invocation of the shell will execute the commands given to at. The jobs on the queue can be examined with option -l ('list') and jobs on the queue can be removed with option -r ('remove') by at:

at -l
81 2001-12-07 15:30 a chris
at -r 81
at -l
$

The fourth column of the listing of jobs contains an a indicating that the queue is named a ('at queue'). You should not normally need to be concerned about which queue a job has been placed on.

Similar to at is batch (which is actually the same as batch is the same as the command at -q b -m now)) This command is used when you do not wish to specify exactly when a job should run, merely that the system load should not be high when you do it. A job submitted with batch is dealt with by the system in exactly the same way as at, except that batch will instruct the time of running to be now and will place the job on a separate queue. Jobs submitted to this batch queue can be listed and removed using at:

batch <testfile
job 121 at 2001-12-07 15:45
at -l
121 2001-12-07 15:45 b chris

Notice that the name of the queue is b ('batch queue')

The 'jobs' described in connection with at and batch should not be confused with the 'jobs' in the section on job control. Jobs in an at-queue can only be created and removed by batch or at.

Worked example

Write a script to list all the files in your filespace, and run that script in one minute's time.
Solution: Using man ls you will discover that option -R ('recursive') to ls will list all files in the current directory and recursively through all subdirectories. So we can pass ls -R as the command to be processed by at. First of all, however, we must change directory to the home directory.

at now + 1 minute
cd
ls -R

ctrl-D


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck