title: question on using bwait to wait for multiple bsub jobs to finish
body: <p>I am new to using LSF (been using PBS/Torque all along). </p>
  
  <p>I need to write code/logic to make sure all bsub jobs finish before other commands/jobs can be fired.</p>
  
  <p>Here is what I have done: I have a master shell script which calls multiple other shell scripts via bsub commands. I capture the job ids from bsub in a log file and I need to ensure that all jobs get finished before the downstream shell script should execute its other commands.</p>
  
  <p>Master shell script</p>
  
  <pre><code>#!/bin/bash
  
  ...Code not shown for brevity..
  
  "Command 1 invoked with multiple bsubs" &gt; log_cmd_1.txt
  
  Need Code logic to use bwait before downstream Commands can be used
  
  "Command 2 will be invoked with multiple bsubs" &gt; log_cmd_2.txt
  
  and so on 
  
  </code></pre>
  
  <p>stdout captured from Command 1 within the Master Shell script is stored in log_cmd_1.txt which looks like this</p>
  
  <pre><code>Submitting Sample 101
  Job &lt;545&gt; is submitted to .
  Submitting Sample 102
  Job &lt;546&gt; is submitted to .
  Submitting Sample 103
  Job &lt;547&gt; is submitted to .
  Submitting Sample 104
  Job &lt;548&gt; is submitted to .
  </code></pre>
  
  <p>I have used the codeblock shown below after Command 1 in the master shell script. </p>
  
  <p>However, it does not seem to work for my situation. Looks like I would have gotten the whole thing wrong below.</p>
  
  <pre><code>while sleep 30m;
  do
      #the below gets the JobId from the log_cmd_1.txt and tries bwait
  
      grep '^Job' &lt;path_to&gt;/log_cmd_1.txt | perl -pe 's!.*?&lt;(\d+)&gt;.*!$1!' | while read -r line; do res=$(bwait -w "done($line)");echo $res; done 1&gt;
  &lt;path_to&gt;/running.txt;
      # the below sed command deletes lines that start with Space
      sed '/^\s*$/d' running.txt &gt; running2.txt;
      # -s file check operator means "file is not zero size"
      if [ -s $WORK_DIR/logs/running2.txt ]
          then
              echo "Jobs still running";
          else
              echo "Jobs complete";
              break;
      fi
  done
  </code></pre>
  
  <p>The question: What's the correct way to do this using bwait within the master shell script. </p>
  
  <p>Thanks in advance.</p>