title: How to launch two threads in bash shell script?
body: <p>I am trying to copy files from <code>machineB</code> and <code>machineC</code> into <code>machineA</code> as I am running my below shell script on <code>machineA</code>.</p>
  
  <p>If the files is not there in <code>machineB</code> then it should be there in <code>machineC</code> for sure so I will try copying the files from <code>machineB</code> first, if it is not there in <code>machineB</code> then I will try copying the same files from <code>machineC</code>.</p>
  
  <p>I am copying the files in parallel using GNU Parallel library and it is working fine. Currently I am copying two files in parallel.</p>
  
  <p>Currently, I am copying the <code>PRIMARY_PARTITION</code> files in <code>PRIMARY</code> folder using GNU parallel and once that is done, then I copy the <code>SECONDARY_PARTITION</code> files in <code>SECONDARY</code> folder using same GNU parallel so it is sequential as of now w.r.t <code>PRIMARY</code> and <code>SECONDARY</code> folder</p>
  
  <p>Below is my shell script and everything works fine -</p>
  
  <pre><code>#!/bin/bash
  
  export PRIMARY=/test01/primary
  export SECONDARY=/test02/secondary
  readonly FILERS_LOCATION=(machineB machineC)
  export FILERS_LOCATION_1=${FILERS_LOCATION[0]}
  export FILERS_LOCATION_2=${FILERS_LOCATION[1]}
  PRIMARY_PARTITION=(550 274 2 546 278) # this will have more file numbers
  SECONDARY_PARTITION=(1643 1103 1372 1096 1369) # this will have more file numbers
  
  export dir3=/testing/snapshot/20140103
  
  # delete primary files first and then copy
  find "$PRIMARY" -mindepth 1 -delete
  
  do_CopyInPrimary() {
    el=$1
    scp david@$FILERS_LOCATION_1:$dir3/new_weekly_2014_"$el"_200003_5.data $PRIMARY/. || scp david@$FILERS_LOCATION_2:$dir3/new_weekly_2014_"$el"_200003_5.data $PRIMARY/.
  }
  export -f do_CopyInPrimary
  parallel -j 2 do_CopyInPrimary ::: "${PRIMARY_PARTITION[@]}"
  
  # delete secondary files first and then copy
  find "$SECONDARY" -mindepth 1 -delete
  
  do_CopyInSecondary() {
    el=$1
    scp david@$FILERS_LOCATION_1:$dir3/new_weekly_2014_"$el"_200003_5.data $SECONDARY/. || scp david@$FILERS_LOCATION_2:$dir3/new_weekly_2014_"$el"_200003_5.data $SECONDARY/.
  }
  export -f do_CopyInSecondary
  parallel -j 2 do_CopyInSecondary ::: "${SECONDARY_PARTITION[@]}"
  </code></pre>
  
  <p><strong>Problem Statement:-</strong></p>
  
  <p>Is there any way I can launch two threads, one to copy files in <code>PRIMARY</code> folder using the same setup as I have above, meaning it will copy two files in parallel. And second thread to copy the files in <code>SECONDARY</code> folder using the same setup as I have above, it should also copy two files parallel simultaneously? </p>
  
  <p>Meaning they should copy files in parallel both in <code>PRIMARY</code> and <code>SECONDARY</code> folder simultaneously not once <code>PRIMARY</code> folder is done, then copy files in <code>SECONDARY</code> folder.</p>
  
  <p>Currently, once <code>PRIMARY</code> folder file is done, then only I try copying the files in <code>SECONDARY</code> folder.</p>
  
  <p>In short, I just need to launch two threads one thread will run this - </p>
  
  <pre><code># delete primary files first and then copy
  find "$PRIMARY" -mindepth 1 -delete
  
  do_CopyInPrimary() {
    el=$1
    scp david@$FILERS_LOCATION_1:$dir3/new_weekly_2014_"$el"_200003_5.data $PRIMARY/. || scp david@$FILERS_LOCATION_2:$dir3/new_weekly_2014_"$el"_200003_5.data $PRIMARY/.
  }
  export -f do_CopyInPrimary
  parallel -j 2 do_CopyInPrimary ::: "${PRIMARY_PARTITION[@]}"
  </code></pre>
  
  <p>And second thread will run this -</p>
  
  <pre><code># delete secondary files first and then copy
  find "$SECONDARY" -mindepth 1 -delete
  
  do_CopyInSecondary() {
    el=$1
    scp david@$FILERS_LOCATION_1:$dir3/new_weekly_2014_"$el"_200003_5.data $SECONDARY/. || scp david@$FILERS_LOCATION_2:$dir3/new_weekly_2014_"$el"_200003_5.data $SECONDARY/.
  }
  export -f do_CopyInSecondary
  parallel -j 2 do_CopyInSecondary ::: "${SECONDARY_PARTITION[@]}"
  </code></pre>
  
  <p>And once all the files are copied successfully, it should echo the message, that all the files are copied. In java, I know how to launch two threads and each thread is performing certain task but not sure how in bash shell script this will work?</p>
  
  <p>My main task is to copy two files in parallel using GNU parallel in <code>PRIMARY</code> folder and <code>SECONDARY</code> folder at a same time?</p>
  
  <p>Is this possible to do in bash shell script?</p>