Wait until a process ends
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Waiting until a process ends means blocking or pausing until a child or external process has finished execution. The exact API depends on the environment, but the underlying idea is always the same: start or identify the process, then wait for its completion and usually inspect its exit status.
The important follow-up question is whether you want to wait forever or with a timeout. In real programs, timeout handling is often just as important as the wait itself.
Wait for a Child Process in a Shell
In a shell script, wait is the standard tool when you started the background process yourself.
$! captures the PID of the most recently started background process. wait then blocks until that process exits.
This is the normal shell pattern when one step must finish before later steps continue.
Wait in Python
In Python, the subprocess module gives you a direct wait() method:
If you want a timeout:
That is the safer production pattern because it prevents the program from waiting forever on a hung child process.
Wait in Java
Java exposes the same idea through Process.waitFor():
And with a timeout:
The timeout pattern matters here too, especially in services and build tools that spawn subprocesses.
Understand Parent-Child Scope
One subtle point is that many "wait" APIs are for child processes your program started itself. Waiting on an arbitrary unrelated PID may require different operating-system-specific APIs or polling logic.
That is why you should separate these questions:
- did my program start the process
- do I only need to wait for completion
- do I also need to capture output or exit status
Those details determine which API is actually appropriate.
If you only have an arbitrary PID in shell scripts, people often fall back to polling:
That is not the same as wait, but it is a practical workaround when the process was not started as a child of the current shell.
Common Pitfalls
The biggest mistake is waiting forever without a timeout in code that talks to external tools or remote resources. If the child process hangs, so does your program.
Another common issue is ignoring the exit code. A process ending is not the same thing as a process succeeding.
It is also easy to deadlock when a child process produces output and the parent never consumes it, especially in APIs that combine waiting with stream handling.
Finally, some wait APIs only work cleanly for child processes, not arbitrary system processes. Make sure the ownership model matches the tool you are calling.
Summary
- Waiting for a process means blocking until it exits and usually reading the exit status.
- Shell scripts use
wait, Python usesPopen.wait(), and Java usesProcess.waitFor(). - Prefer timeout-capable waits in real applications.
- Treat exit-code checking as part of the wait logic.
- Make sure the API matches whether the process is your child or just some external PID.

