Stop worrying if your background jobs actually finished

Shell Scripting / Bash Tricks

Stop worrying if your background jobs actually finished

🧩 The Challenge

You trigger a bunch of heavy cleanup scripts in the background and then realize you have no easy way to wait for the stragglers without writing a complex loop. I’ve wasted so much time polling processes just to see if the directory is finally clear.

💡 The Fix

Use the built-in wait command with a process ID or just let it pause your script until everything in the current session hits the finish line. It saves you from writing those janky sleep-retry loops that never seem to handle edge cases correctly.

./cleanup_part1.sh & ./cleanup_part2.sh & wait
echo "Everything is done, proceeding with the next step."

⚙️ Why It Works

Calling wait without arguments blocks your shell until every background task started in that session has exited. You can also pass specific PIDs if you only care about waiting for one or two specific processes to wrap up.

🚀 Pro-Tip: Store the PID of your background task immediately with $! so you can selectively wait on specific jobs.

Linux Tips & Tricks | © ngelinux.com | 9/29/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted