Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I launch a process in background and check when it ends within a bash script? My idea is a script like this:

launch backgroundprocess &
while [ Process is running ];do

share|improve this question

migrated from serverfault.com May 22 '13 at 16:53

This question came from our site for system and network administrators.

up vote 23 down vote accepted

The key is the "wait" command:


/my/process &
/another/process &
echo "All processes done!"
share|improve this answer
Benefit of this: it does NOT burn CPU time with an infinite loop running all the time.... ;) – Pascal Schmiel May 22 '13 at 12:47
The disadvantage of this is that you can't do anything else while you are waiting, such as indicating progress to the user. Only cuonglm's solution allows you to do this. – Mark Booth Apr 28 '15 at 10:30

With wait you can have the granularity you need:

sleep 1 &
sleep 2 &

wait $PID1
echo PID1 has ended.
echo All background processes have exited.
share|improve this answer

Here is one way to do it:

launch backgroundprocess &

while kill -0 "$PROC_ID" >/dev/null 2>&1; do
exit 0
share|improve this answer
Thanks, this is definitely the only answer which answers my interpretation of the original question (and the reason why I ended up looking at this question) – Mark Booth Apr 30 '15 at 14:48

You can run your process with nohup and write shell script to read nohup.out file which nohup uses to log .

  nohup command &
share|improve this answer
(1) At the risk of splitting hairs, nohup doesn't write anything to nohup.out; it merely creates the file, and redirects the output of the command to it.  (2) If the command doesn't produce any output, nothing will be written to nohup.out, and this idea goes nowhere fast.  (3) Even if command does write output, how can you tell when it ends by monitoring that output? – G-Man Oct 23 '15 at 13:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.