The atd Daemon
The atd daemon allows users to submit jobs to be performed at a later time, such as “at 2:00am”. In order to use the atd daemon, it must be running. Users can confirm that atd is running simply by examining a list of running processes:
$ ps aux | grep atd daemon 4730 0.0 0.2 1420 532 ? S 15:42 0:00 /usr/sbin/atd madonna 5570 0.0 0.2 3572 640 pts/2 S 16:43 0:00 grep atd
Notice that the seventh column specifies what terminal a process is associated with. For blondie’s grep command, the terminal is pts/2, which probably refers to a network shell or a graphical terminal within an X session. Notice that the atd daemon has no associated terminal. One of the defining characteristics of a daemon is that it drops its association with the terminal that started it.
Submitting Jobs with at
The at command is used to submit jobs to the atd daemon to be run at a specific time. The commands to be run are either submitted as a script (with the -f command line switch), or entered directly via stdin. Standard out from the command is mailed to the user.
at [-f filename | -m] TIME
|-f filename||run the script specified by filename|
|-m||Notify the user by email when done, even if there is no output.|
The time of day can be specified using HH:MM, suffixed by “am” or “pm”. The terms “midnight”, “noon”, and “teatime” can also be used. (You read correctly, “teatime”.) A date can also be specified using several formats, including MM/DD/YY. The at(1) man page provides many more details.
The wrestler hogan would like to print a file containing all of the fan mail that he has received, fanmail.txt. He’s a little concerned, though, because he shares the printer with ventura, who uses the printer a lot as well. Wanting to avoid a fight, hogan decides to delay his printing until 2:00 in the morning.
$ at 2:00 am warning: commands will be executed using (in order) a) $SHELL b) login shell c) /bin/sh at> lpr fanmail.txt at> CTRL-D job 7 at 2020-06-17 02:00
Because hogan did not use the -f command line switch, the at command prompted hogan to type in his commands using stdin (the keyboard). Fortunately, hogan knows that CTRL-D, when entered directly from a terminal, indicates an “end of file”. Alternately, he could have piped the command into stdin directly:
$ echo "lpr fanmail" | at 2:00 am warning: commands will be executed using (in order) a) $SHELL b) login shell c) /bin/sh job 7 at 2003-06-17 02:00
Next, hogan confirms that his job has been registered using atq.
$ atq 7 2003-06-17 02:00 a hogan
Lastly, hogan remembers that ventura is on vacation, so he can print his fan mail without incident. He decides to cancel his at job, and print the file directly.
$ atrm 7 $ atq $ lpr fanmail.txt
Delaying Tasks with batch
The batch command, like the at command, is used to defer tasks until a later time. Unlike the at command, batch does not run the command at a specific time but instead waits until the system is not busy with other tasks, whenever that time might be. If the machine is not busy when the job is submitted, the job might run immediately. The atd daemon monitors the system’s loadavg, and waits for it to drop beneath 0.8 before running the job.
The batch command has a syntax identical to the at command, where jobs can either be specified using stdin, or submitted as a batch file with the -f command-line switch. If a time is specified, batch will delay observing the machine until the specified time. At that time, batch will begin monitoring the system’s loadavg, and run the job when the system is not otherwise busy.