Master Linux sleep and wait: Precise Timing and Process Synchronization
This guide explains how to use the Linux sleep and wait commands for pausing execution, specifying time units, handling sub‑second intervals, running periodic tasks, and synchronizing background processes, complete with practical Bash examples and detailed explanations.
Sleep command
The sleep command pauses execution for a specified duration. It accepts a numeric argument interpreted as seconds by default, but you can add suffixes to specify minutes ( m), hours ( h), or days ( d). Sub‑second delays are expressed by prefixing a decimal point (e.g., .1 for one‑tenth of a second). date; sleep 30; date In the example above, sleep 30 inserts a 30‑second pause between two date commands. date; sleep 10s; date Common suffixes:
10s = 10 seconds
1m = 1 minute
2h = 2 hours
3d = 3 days
Sub‑second examples:
.1 = one‑tenth of a second
.01 = one‑hundredth of a second
.001 = one‑thousandth of a second date; sleep .1; date Typical use cases include running a command at regular intervals, such as every 10 seconds or every minute, often inside a loop to monitor system activity. while true; do who; sleep 100; done Another example shows monitoring memory usage periodically:
while true; do free -h; sleep 5; doneWait command
The wait command pauses the script until one or more background jobs finish and then returns their exit status. It works only with child processes started by the current shell.
#!/bin/bash
sleep 5 &
process_id=$!
echo "PID: $process_id"
wait $process_id
echo "Exit status: $?"The special option -n makes wait return as soon as any background job finishes, without needing a specific PID.
#!/bin/bash
sleep 15 &
sleep 9 &
sleep 6 &
wait -n
echo "First job has been completed."Chaining multiple wait -n calls lets you react to each job in the order they finish, which is the reverse of their start order when durations differ.
#!/bin/bash
sleep 15 &
sleep 9 &
sleep 6 &
wait -n
echo "First job has been completed."
wait -n
echo "Next job has been completed."
wait
echo "All jobs have been completed."Conclusion
Both sleep and wait can be used in interactive shells or within scripts. sleep simply delays execution, while wait synchronizes with background processes and reports their exit status, making it essential for coordinating multiple concurrent tasks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
