4

Say I have the following bash script:

#!/usr/bin/env bash
process-1&
process-2&
process-3&

I have two questions:

  1. how can I check at the end of my script that all processes exited with an exit code of 0 ?
  2. how can my script "wait" until all launched processes have finished and then do a concluding echo (perhaps informing the user of the exit codes)?
  • Couldn't you can use wait on each process sequentially? – jw013 Aug 20 '14 at 17:01
1

You could use the bash builtin wait.
Three times wait -n, checking the exit status each time.

From man bash:

    wait [-n] [n ...]
           Wait  for  each  specified child process and return its termination
           status.  Each n may be a process ID or a job  specification;  if  a
           job  spec is given, all processes in that job's pipeline are waited
           for.  If n is not given, all currently active child  processes  are
           waited  for,  and  the  return status is zero.  If the -n option is
           supplied, wait waits for any job to terminate and returns its  exit
           status.   If  n specifies a non-existent process or job, the return
           status is 127.  Otherwise, the return status is the exit status  of
           the last process or job waited for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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