Three kinds of delay
A delayed job waits for a time, a condition, or an event before it runs. A timed job starts at a specific moment, such as 18:00 tomorrow. A conditional job starts when the system load is low, which is what batch is for. An event job starts after another command finishes. The shell can express the second and third kinds with wait and with &&, while cron and at express the first kind with a clock.
The choice matters because each tool has a different failure mode. A job tied to a clock is missed if the machine is off at that moment, unless the tool catches up. A job started in the background of your shell is lost when the shell exits, unless you protect it as the nohup lesson showed.
The at command
The at command reads a job from standard input and runs it at a time you give. It keeps the job in a queue that is managed by a daemon, so the job survives your logout. Typical usage is at followed by a time such as now plus five minutes, or 18:00. The batch command is related, and it runs a job when the load average drops below a threshold.
The at command is not installed on this lab machine, and the daemon that runs its queue is not running either. On a system that has it, you can ask at to list the queue with atq. The usual reason for a missing at is that minimal images leave it out, so check with command -v before you rely on it.
command -v at || echo "at is not installed here"Delay in the shell
A subshell with a sleep can run a command later, without any scheduler. The parentheses create the child, sleep waits, and the command runs when the sleep ends. The ampersand moves the whole group to the background, so the terminal is free for other work.
(sleep 2; echo later > later.txt) &
wait
cat later.txtThe wait builtin blocks until the background group has finished, which makes the example deterministic. Without the wait, the cat would run before the file exists. The pattern is fine for a short delay in a script, but it ends when the shell exits, so it is not a substitute for a scheduler.
Limiting a job with timeout
The timeout command runs another command and stops it after a time limit. If the limit is reached, it sends a signal, which is SIGTERM by default, and it exits with status 124. The status is a useful signal in its own right, because a script can distinguish a job that timed out from one that failed.
timeout 2 sleep 10; echo "status=$?"The sleep is set to run for ten seconds, but timeout stops it after two. The status line prints 124, which means the command was cut short. Use timeout for any job that might hang, such as a network request, so that one stuck command does not block the rest of a schedule.
timeout can send a different signal with the -s option, and it can send KILL after a grace period with -k. A program that catches SIGTERM may need the stronger signal to stop, which the signals lesson of the previous module described.
Reporting that a delayed job ran
A one-off job should leave a record that it ran and how it ended. The simplest record is a line in a log file that includes the status. Write the log line in the same command, so that the record exists even when the job fails partway. This is the same habit as the cron redirection in the previous unit, applied to a single job.
Choosing a tool
Use cron for a repeated job with a fixed schedule. Use at for one job at a set time on a system that has it. Use a backgrounded subshell with a wait for a short delay inside a script. Use timeout to put a limit on any command that might not finish. Use batch when the job should run only when the machine is idle. The right answer is usually the one with the fewest moving parts that still survives the events you care about.
A delayed job also needs a plan for failure. If the delay is long, the machine may reboot before the job runs, and an in-memory sleep is lost with it. For work that must happen after a reboot, a scheduled entry that the system reads at boot is the right tool, while a subshell is only for work that is short enough to finish in the same session. Decide which case you are in before you write the job, because the two cases need different tools.
Timeouts need the same care. A timeout that is too short kills good work, and a timeout that is too long hides a hang until the schedule has already slipped. Measure a typical run first, and set the limit at a comfortable multiple of that time. Record the limit in the script next to the timeout call, so that the next person who reads the job knows why it is set to that value.
Hands-on: delay and limit
Run a delayed write and a timed-out sleep, and save the results in files. The first file proves that the delayed job ran, and the second records the exit status of the limited job.
(sleep 1; echo delayed > delayed.txt) &
wait
timeout 1 sleep 5; echo "status=$?" > timeout.txt
cat delayed.txt timeout.txtThe file delayed.txt holds one word, and timeout.txt holds the status line. The status should be 124. Those two results are the pattern to check whenever you write a one-off job.