Tuesday, February 05, 2008

Linux : Executes commands at a specified time

at [options] time [date | +increment]

atq

atrm job-list

batch [options] [time]


The at and batch utilities execute commands at a specified time. They accept commands from standard input or, with the –f option, from a file. Commands are executed in the same environment as the at or batch command. Unless redirected, standard output and standard error from commands are emailed to the user who ran at or batch. A job is the group of commands that is executed by one call to at. The batch utility differs from at in that it schedules jobs so that they run when the CPU load on the system is low.

The atq utility displays a list of at jobs you have queued; atrm cancels pending at jobs.

Arguments

The time is the time of day that at runs the job. You can specify the time as a one-, two-, or four-digit number. One- and two-digit numbers specify an hour, and four-digit numbers specify an hour and minute. You can also give the time in the form hh:mm. The at utility assumes a 24-hour clock unless you place am or pm immediately after the number, in which case it uses a 12-hour clock. You can also specify time as now, midnight, noon, or teatime (4:00 PM).

The date is the day of the week or day of the month on which you want at to execute the job. When you do not specify a day, at executes the job today if the hour you specify in time is greater than the current hour. If the hour is less than the current hour, at executes the job tomorrow.

You specify a day of the week by spelling it out or abbreviating it to three letters. You can also use the words today and tomorrow. Use the name of a month followed by the number of the day in the month to specify a date. You can follow the month and day number with a year.

The increment is a number followed by one of the following (plural or singular is allowed): minutes, hours, days, or weeks. The at utility adds the increment to time. You cannot specify an increment for a date.

When using atrm, job-list is a list of one or more at job numbers. You can list job numbers by running at with the –l option or by using atq.

Options

The –l and –d options are not for use when you initiate a job with at. You can use them only to determine the status of a job or to cancel a job.

–c job-list

(cat) Displays the environment and commands specified by job-list.

–d job-list

(delete) Cancels jobs that you previously submitted with at. The job-list argument is a list of one or more at job numbers to cancel. If you do not remember the job number, use the –l option or run atq to list your jobs and their numbers. Using this option with at is the same as running atrm.

–f file

(file) Specifies that commands come from file instead of standard input. This option is useful for long lists of commands or commands that are executed repeatedly.

–l

(list) Displays a list of your at jobs. Using this option with at is the same as running atq.

–m

(mail) Sends you email after a job is run, even when nothing is sent to standard output or standard error. When a job generates output, at always emails it to you, regardless of this option.


Notes

The shell saves the environment variables and the working directory at the time you submit an at job so that they are available when at executes commands.

/etc/at.allow and /etc/at.deny

The root user can always use at. The /etc/at.allow and /etc/at.deny files, which should be read0able and writable by root only (mode 600), control which ordinary, local users can use at. When /etc/at.deny exists and is empty, all users can use at. When /etc/at.deny does not exist, only users listed in /etc/at.allow can use at. Users listed in /etc/at.deny cannot use at unless they are also listed in /etc/at.allow.

Jobs you submit using at are run by the at daemon (atd). This daemon stores jobs in /var/spool/at and output in /var/spool/at/spool, both of which should be set to mode 700 and owned by the user named daemon.

Examples

You can use any of the following techniques to paginate and print long_file tomorrow at 2:00 AM. The first example executes the command directly from the command line; the last two examples use the pr_tonight file, which contains the necessary command, and execute it using at.

$ at 2am

at> pr long_file | lpr

at>CONTROL-D

job 8 at 2005-08-17 02:00



$ cat pr_tonight

#!/bin/bash

pr long_file | lpr



$ at -f pr_tonight 2am

job 9 at 2005-08-17 02:00



$ at 2am <>

job 10 at 2005-08-17 02:00


If you execute commands directly from the command line, you must signal the end of the commands by pressing CONTROL-D at the beginning of a line. After you press CONTROL-D, at displays a line that begins with job followed by the job number and the time at will execute the job.

If you run atq after the preceding commands, it displays a list of jobs in its queue:

$ atq

8 2005-08-17 02:00 a

9 2005-08-17 02:00 a

10 2005-08-17 02:00 a


The following command removes job number 9 from the queue:

$ atrm 9

$ atq

8 2005-08-17 02:00 a

10 2005-08-17 02:00 a


The next example executes cmdfile at 3:30 PM (1530 hours) one week from today:

$ at -f cmdfile 1530 +1 week

job 12 at 2005-08-23 15:30


Next at executes a job at 7 PM on Thursday. This job uses find to create an intermediate file, redirects the output sent to standard error, and prints the file.

$ at 7pm Thursday

at> find / -name "core" -print >report.out 2>report.err

at> lpr report.out

at>CONTROL-D

job 13 at 2005-08-18 19:00


The final example shows some of the output generated by the –c option when at is queried about the preceding job. Most of the lines show the environment; only the last few lines execute the commands:

$ at -c 13

#!/bin/sh

# atrun uid=500 gid=500

# mail mark 0

umask 2

PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:.;

export PATH

PWD=/home/mark/book.examples/99/cp; export PWD

EXINIT=set\ ai\ aw; export EXINIT

LANG=C; export LANG

PS1=\\\$\ ; export PS1

...

cd /home/mark/book\.examples/99/cp || {

echo 'Execution directory inaccessible' >&2

exit 1

}

find / -name "core" -print >report.out 2>report.err

lpr report.out

1 comment:

Unknown said...

Thanks for this article, it helped.
I stil have to find out how is it with mailing of results and errors. Can I set the mail somewhere?