$_ CLI In Depth
Learning progress 0%
lesson packages services cron

One-Off and Delayed Jobs

5 min read Module 11: Packages, Services & Scheduled Jobs Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:11

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.

BASH1 lines
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.

BASH3 lines
(sleep 2; echo later > later.txt) &
wait
cat later.txt

The 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.

BASH1 lines
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.

Note

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.

BASH4 lines
(sleep 1; echo delayed > delayed.txt) &
wait
timeout 1 sleep 5; echo "status=$?" > timeout.txt
cat delayed.txt timeout.txt

The 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.

Terminal lab · 2 steps · real Linux in your browser

Limit a job and run a delayed command

Use timeout to stop a sleep and record status 124, then run a delayed write and wait for it.

01
Stop a sleep with timeout
Run sleep for five seconds under a one-second limit. Save the exit status in timeout.txt in the form status=N. ``bash timeout 1 sleep 5; echo "status=$?" > timeout.txt `` The status should be 124, which means timeout stopped the command.
Show hint for step 1
Put timeout 1 in front of sleep 5. Then echo the status right after.
02
Run a delayed write and wait for it
Put a group in parentheses, sleep one second, then write the word delayed into delayed.txt. Send the group to the background and wait for it. ``bash (sleep 1; echo delayed > delayed.txt) & wait `` After wait returns, delayed.txt holds the word delayed.
Show hint for step 2
The ampersand goes after the closing parenthesis. The wait builtin must come after it.
Check yourself Question 1 of 4
Concept Check Exit status of timeout recall

Which exit status does timeout use when it stops a command because the time limit was reached?

Recap & Key Takeaways

at and batch run one-off jobs from a managed queue, but this lab machine has neither at nor its daemon.
A subshell with sleep, followed by wait, runs a short delayed command inside a script.
timeout stops a command after a limit and exits with status 124, which lets a script tell a timeout from a failure.
Choose the tool with the fewest moving parts that still survives the events you care about.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue