title: Bash script invoked by "mpirun" calls another MPI executable and crashes with exit code 13
body: <p>I have two executables: <code>prog1</code> and <code>prog2</code>.</p>
  
  <p>These executables read input data and then do some calculations on it. Depending on the "type" of input data, either <code>prog1</code> or <code>prog2</code> have to be used.</p>
  
  <p>I would like to simplify this for the user who should just have to call <code>prog</code>, which is a shell script that </p>
  
  <ol>
  <li>checks on the data (by running <code>prog1</code>),</li>
  <li>runs <code>prog1</code> or <code>prog2</code> with <code>exec</code>.</li>
  </ol>
  
  <p>For serial programs (no MPI), this works well. In the case of MPI, I want to call <code>prog</code> with <code>mpirun -n 2 prog</code>. Then, the shell script is invoked by the two processes. This fails if step (1) involves running another MPI executable.</p>
  
  <p>A simple solution would be to compile a serial version of <code>prog1</code> (for step 1) and use this version instead. However, it would be much simpler for me if I could just use the MPI executable that I already have.</p>
  
  <p>Below, I give a very simple testcase.</p>
  
  <p>Thanks in advance!</p>
  
  <hr>
  
  <p>I have found similar questions on the internet, but none really helped me.</p>
  
  <p>The closest one is from an intel forum:
  <a href="https://software.intel.com/en-us/forums/intel-clusters-and-hpc-technology/topic/754976" rel="nofollow noreferrer">https://software.intel.com/en-us/forums/intel-clusters-and-hpc-technology/topic/754976</a>
  (unfortunately with no replies)</p>
  
  <p>This is a minimal MPI program (<code>run.f90</code>):</p>
  
  <pre><code>program run
  
  use mpi
  
  implicit none
  
  integer::num_process,rank,ierr;
  
  call MPI_Init(ierr);
  call MPI_Comm_rank(MPI_COMM_WORLD, rank,ierr);
  call MPI_Comm_size(MPI_COMM_WORLD, num_process,ierr);
  
  write(*,*) 'I am',rank
  
  call MPI_Finalize(ierr);
  
  end program
  </code></pre>
  
  <p>This is a simple serial program (<code>simple.f90</code>)</p>
  
  <pre><code>program test
  implicit none
  write(*,*) 'hello'
  end
  </code></pre>
  
  <p>This is a simple shell script (<code>script.sh</code>) that first gets some data from <code>simple</code> or <code>run</code> and then executes <code>run</code></p>
  
  <pre><code>#!/bin/sh
  #data=`simple` # this works
  data=`run` # this does not work
  echo "exec run..."
  exec run
  </code></pre>
  
  <p>Compilation:</p>
  
  <pre><code>mpiifort run.f90 -o run
  mpiifort simple.f90 -o simple
  </code></pre>
  
  <p>Run <code>script.sh</code> through <code>mpirun</code>:</p>
  
  <p>The command <code>mpirun -n 2 script.sh</code> fails with</p>
  
  <pre><code>exec run...
  exec run...
  
  ===================================================================================
  =   BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
  =   PID 21258 RUNNING AT iff822
  =   EXIT CODE: 13
  =   CLEANING UP REMAINING PROCESSES
  =   YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
  ===================================================================================
  YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Broken pipe (signal 13)
  This typically refers to a problem with your application.
  Please see the FAQ page for debugging suggestions
  </code></pre>
  
  <p>With <code>simple</code> instead of <code>run</code> in <code>script.sh</code>, the command <code>mpirun -n 2 script.sh</code> works</p>
  
  <pre><code>exec run...
  exec run...
   I am           1
   I am           0
  </code></pre>