title: Check running status of a background process launched from same bash script
body: <p>I have to write a bash script that launches a process in <strong>background</strong> in accordance to command line argument passed and <strong>returns if it were successfully able to <strike>run</strike> launch the program.</strong></p>
  
  <p>Here is a pseudo code of what I am trying to achieve</p>
  
  <pre><code>if [ "$1" = "PROG_1" ] ; then
      ./launchProg1 &amp;
      if [ isLaunchSuccess ] ; then
          echo "Success"
      else
          echo "failed"
          exit 1
      fi
  elif [ "$1" = "PROG_2" ] ; then
      ./launchProg2 &amp;
      if [ isLaunchSuccess ] ; then
          echo "Success"
      else
          echo "failed"
          exit 1
      fi
  fi
  </code></pre>
  
  <p>Script cannot <code>wait</code> or <code>sleep</code> since it will be called by another mission critical c++ program and needs high throughput ( wrt no of processes started per second ) and moreover running time of processes are unknown.  Script neither needs to capture any input/output nor waits for launched process' completion. </p>
  
  <p>I have unsuccessfully tried the following:</p>
  
  <pre><code>#Method 1
  if [ "$1" = "KP1" ] ; then
      echo "The Arguement is KP1"
      ./kp 'this is text' &amp;
      if [ $? = "0" ] ; then
          echo "Success"
      else
          echo "failed"
          exit 1
      fi
  elif [ "$1" = "KP2" ] ; then
      echo "The Arguement is KP2"
      ./NoSuchCommand 'this is text' &amp;
      if [ $? = "0" ] ; then
          echo "Success"
      else
          echo "failed"
          exit 1
      fi
  #Method 2
  elif [ "$1" = "CD5" ] ; then
      echo "The Arguement is CD5"
      cd "doesNotExist" &amp;
      PROC_ID=$!
      echo "PID is $PROC_ID"
      if kill -0 "$PROC_ID" ; then
          echo "Success"
      else
          echo "failed"
          exit 1
      fi
  #Method 3
  elif [ "$1" = "CD6" ] ; then
      echo "The Arguement is CD6"
      cd .. &amp;
      PROC_ID=$!
      echo "PID is $PROC_ID"
      ps -eo pid | grep "$PROC_ID" &amp;&amp; { echo "Success"; exit 0; }
      ps -eo pid | grep  "$PROC_ID" || { echo "failed" ; exit 1; }
  else
      echo "Unknown Argument"
      exit 1
  fi
  </code></pre>
  
  <p>Running the script gives unreliable output. Method 1, 2 always return <code>Success</code> while Method 3 returns <code>failed</code> when process execution finishes before the checks. </p>
  
  <p>Here is sample tested on <code>GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)</code> and <code>GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)</code></p>
  
  <pre><code>[scripts]$ ./processStarted3.sh KP1
  The Arguement is KP1
  Success
  [scripts]$ ./processStarted3.sh KP2
  The Arguement is KP2
  Success
  ./processStarted3.sh: line 13: ./NoSuchCommand: No such file or directory
  [scripts]$ ./processStarted3.sh CD6
  The Arguement is CD6
  PID is 25050
  failed
  </code></pre>
  
  <p>As suggested in similar questions, I cannot use process names as <a href="https://stackoverflow.com/a/6225730/780190">one process may be executed several times</a> and <a href="https://stackoverflow.com/a/1570356/780190">others</a> can't be applied.</p>
  
  <p>I have not tried <a href="http://linux.die.net/man/1/screen" rel="nofollow noreferrer">screen</a> and <a href="http://linux.die.net/man/1/tmux" rel="nofollow noreferrer">tmux</a>, since getting permission to install them on production servers wont be easy ( but will do so if that is the only option left )</p>
  
  <p><strong>UPDATE</strong><br>
  @ghoti<br>
  <code>./kp</code> is program which exists and launching the program returns <code>Success</code>. <code>./NoSuchCommand</code> does not exist. Still as you can see from <em>(edited)</em> output, script incorrectly returns <code>Success</code>. </p>
  
  <p><strong>It does not matter when the process completes execution or program abnormally terminates</strong>. Programs <strong>launched</strong> via script are not tracked in any way ( hence we do not store <code>pid</code> in any table nor necessity arises to use <code>deamontools</code> ).</p>
  
  <p>@Etan Reisner<br>
  Example of a program which fails to <strong>launch</strong> will be <code>./NoSuchCommand</code>,which does not exist. Or maybe a corrupted program which fails to start.</p>
  
  <p>@Vorsprung<br>
  Calling a script which launches a program in background does not take alot of time ( and is manageable as per our expectations). But <code>sleep 1</code> will accumulate over time to cause issues.</p>
  
  <p>Aforementioned <code>#Method3</code> works fine barring processes which terminate before <code>ps -eo pid | grep "$PROC_ID" &amp;&amp; { echo "Success"; exit 0; }</code> check can be performed.</p>