🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

📜 Shell Scripting March 15, 2026 5

Linux Command: wait

Wait for background processes to finish

Terminal — Shell Scripting
Command
$ cmd1 & cmd2 & cmd3 & wait; echo "All done"
Output
Runs 3 commands in parallel and waits for all to finish.

wait pauses the shell until specified background processes complete. Without arguments, it waits for all background jobs. With a PID or job ID, it waits for that specific process. wait returns the exit status of the waited-for process, making it essential for parallel processing scripts that need to check if background tasks succeeded or failed. wait is commonly used with background (&) processes, parallel command execution, and coordinating multiple asynchronous operations in shell scripts.

Syntax

wait [PID]...

Common Examples

  • cmd1 & cmd2 & cmd3 & wait; echo "All done" — Runs 3 commands in parallel and waits for all to finish.
  • long_task & PID=$!; echo "Started $PID"; wait $PID; echo "Exit code: $?" — Starts a task, captures its PID, waits for it, and checks its exit status.
  • task1 & P1=$!; task2 & P2=$!; wait $P1 || echo "task1 failed"; wait $P2 || echo "task2 failed" — Runs tasks in parallel and checks each exit code independently.
  • for i in 1 2 3 4 5; do slow_task $i & done; wait -n; echo "First one done" — Starts multiple jobs and continues as soon as any one finishes.

Pro Tips

    Master this and 230+ other Linux commands with our comprehensive eBooks and cheat sheets.

    Share this tip