Fundamentals 5 min read

Mastering the Linux ‘wait’ Command: Synchronize Processes and Capture Exit Status

This guide explains how the Linux wait command pauses script execution until specified processes or jobs finish, details its PID/JID options, exit‑status conventions, and provides step‑by‑step script examples that illustrate proper usage versus omitting wait.

ITPUB
ITPUB
ITPUB
Mastering the Linux ‘wait’ Command: Synchronize Processes and Capture Exit Status

How the wait command works

The wait command is a built‑in shell utility that pauses execution until a given process ID (PID) or job ID (JID) terminates, then returns that process's exit status. It is useful in large automation pipelines where later modules must wait for earlier ones to finish and provide data.

Typical usage examples include wait 13245 to wait for process 13245, or wait %1 (or wait $!) to wait for a background job identified by its job ID. The command returns the exit status of the last specified PID/JID; if a process ends abnormally, the status is greater than 128. A return value of 0 indicates all waited‑for processes exited cleanly, while values 1‑126 signal errors, and 127 means the specified PID was unknown.

Examples

Example 1 – Script with wait

Two scripts, foo.sh and bar.sh, demonstrate proper synchronization. foo.sh prints a random number between 1 and 5. bar.sh launches foo.sh in the background, captures its PID, then executes wait $! before proceeding, ensuring the output of foo.sh is available before bar.sh continues.

Example 2 – Script without wait

The same two scripts run, but bar.sh starts foo.sh in the background without a wait. Both scripts run concurrently, so bar.sh does not wait for foo.sh to finish, leading to interleaved or missing output.

Example 3 – Script with wait and exit‑status handling

In this scenario, bar.sh again runs foo.sh in the background, waits for it, and then checks the exit status returned by wait. If foo.sh exits with a non‑zero status, bar.sh can react accordingly, demonstrating how to propagate error codes through synchronized scripts.

Conclusion

Both wait and sleep are time‑based system calls, but they serve different purposes: wait synchronizes based on process termination, while sleep merely pauses for a fixed duration. Understanding the exit‑status semantics of wait enables reliable orchestration of dependent shell tasks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

waitprocess synchronization
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.