Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem you have is that you cannot wait for one of multiple background processes to complete. If you observe job status (using jobs) then finished background jobs are removed from the job list. You need another mechanism to determine whether a background job has finished.</p> <p>The following example uses starts to background processes (sleeps). It then loops using ps to see if they are still running. If not it uses wait to gather the exit code and starts a new background process.</p> <pre><code>#!/bin/bash sleep 3 &amp; pid1=$! sleep 6 &amp; pid2=$! while ( true ) do running1=`ps -p $pid1 --no-headers | wc -l` if [ $running1 == 0 ] then wait $pid1 echo process 1 finished with exit code $? sleep 3 &amp; pid1=$! else echo process 1 running fi running2=`ps -p $pid2 --no-headers | wc -l` if [ $running2 == 0 ] then wait $pid2 echo process 2 finished with exit code $? sleep 6 &amp; pid2=$! else echo process 2 running fi sleep 1 done </code></pre> <p>Edit: Using SIGCHLD (without polling):</p> <pre><code>#!/bin/bash set -bm trap 'ChildFinished' SIGCHLD function ChildFinished() { running1=`ps -p $pid1 --no-headers | wc -l` if [ $running1 == 0 ] then wait $pid1 echo process 1 finished with exit code $? sleep 3 &amp; pid1=$! else echo process 1 running fi running2=`ps -p $pid2 --no-headers | wc -l` if [ $running2 == 0 ] then wait $pid2 echo process 2 finished with exit code $? sleep 6 &amp; pid2=$! else echo process 2 running fi sleep 1 } sleep 3 &amp; pid1=$! sleep 6 &amp; pid2=$! sleep 1000d </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload