title: Issues with Runtime.exec Process.getInputStream and Process.waitFor()
body: <p>I have written a code to check whether HSQL, JBoss, Radius (AAA sever) and MySQL is running or not.The code is written in an inifinte while loop to continuously monitor.Now I tested that if this service (let's say hsql) is running then <code>/bin/bash -c ps -ef | grep 'hsql' | wc -l</code> will return 3 when this is passed as an argument in <code>Runtime.exec()</code> method.  Now I have come to know that the inputstream or errstream of the forked process must not be overflowed.  Otherwise it'll cause deadlock.</p>
  
  <p>Keeping that in mind I have written this code:</p>
  
  <pre><code>import java.io.BufferedReader;  
  import java.io.IOException;  
  import java.io.InputStreamReader;  
  import java.util.Date;
  
  public class MonitorHsqlJBossRadius {  
  
      public static void main(String[] args) {  
          String hsqlCmd[] = {"/bin/bash","-c","ps -ef | grep 'hsql' | wc -l "};  
          String jbossCommand[] = {"/bin/sh","-c","ps -ef | grep 'jboss' | wc -l"};  
          String radiusCommand[] = {"/bin/sh","-c","ps -ef | grep 'radius' | wc -l"};  
          String mySqlCommand[] = {"/bin/sh","-c","/etc/init.d/mysqld status"};  
          String line = null;  
          String mobNo = "(obscured)";  
          Process process = null;  
          Runtime runtime = Runtime.getRuntime();  
          BufferedReader reader = null;  
          SendSMS sender = new SendSMS();  
          boolean sendMsgHsql = false;  
          boolean sendMsgJBoss = false;  
          boolean sendMsgRadius = false;  
          int itr = 1;  
          while(true)  
          {  
              try
              {  
                  process = runtime.exec(hsqlCmd);  
                  reader = new BufferedReader(new InputStreamReader(process.getInputStream()));  
                  while((line=reader.readLine())!=null)  
                  {  
                      if (!line.equals("3"))  
                          sendMsgHsql = true;  
                  }  
  
                  process.waitFor();  
                  if (sendMsgHsql)  
                  {  
                      //sender.sendSMS("HSQL is not running "+(new Date().toString()), mobNo,1,itr);  
                      System.out.println("HSQL is not running "+itr);  
                  }  
                  else  
                      System.out.println("HSQL is running "+itr);  
  
                  sendMsgHsql = false;  
  
                  process = runtime.exec(jbossCommand);  
                  reader = new BufferedReader(new InputStreamReader(process.getInputStream()));  
                  while((line=reader.readLine())!=null)  
                  {  
                      if (!line.equals("3"))  
                         sendMsgJBoss = true;  
                  }  
  
                  process.waitFor();  
                  if (sendMsgJBoss)  
                  {  
                      //sender.sendSMS("JBoss is not running "+(new Date().toString()), mobNo,2,itr);  
                      System.out.println("JBoss is not running "+itr);  
                  }  
                  else  
                      System.out.println("JBoss is running "+itr);  
  
                  sendMsgJBoss = false;  
  
                  process = runtime.exec(radiusCommand);  
                  reader = new BufferedReader(new InputStreamReader(process.getInputStream()));  
                  while((line=reader.readLine())!=null)  
                  {  
                      if(!line.equals("3"))  
                          sendMsgRadius = true;  
                  }  
  
                  process.waitFor();  
                  if (sendMsgRadius)  
                  {  
                      //sender.sendSMS("Radius is not running "+(new Date().toString()), mobNo,3,itr);  
                      System.out.println("Radius is not running "+itr);  
                  }  
                  else  
                      System.out.println("Radius is running "+itr);  
  
                  sendMsgRadius = false;  
              }   
  
  
              catch (IOException e) {  
                  e.printStackTrace();  
              }   
              catch (InterruptedException e) {  
                  e.printStackTrace();  
              }
  
              try  
              {  
                  System.out.println("\n--------- "+itr+" ------------\n");  
                  itr++;  
                  Thread.sleep(1000*5);  
              }  
              catch (InterruptedException e) {  
                  e.printStackTrace();  
              }  
          }
      }
  }  
  </code></pre>
  
  <p>The output I'm expecting is as follows</p>
  
  <pre>
  HSQL is running/not running 1
  JBoss is running/not running 1
  MySQL is running/not running 1
  -------------------1----------------
  
  HSQL is running/not running 2
  JBoss is running/not running 2
  MySQL is running/not running 2
  -------------------2----------------
  </pre>
  
  <p>and so on...</p>
  
  <p>Howevere I'm not getting output like that.  Sometimes HSQL's status message is not printed or sometimes JBoss status message is not printed.  Even sometimes I get output like</p>
  
  <pre>
  ------------------1----------------------
  </pre>
  
  <p>after this status messages are printed.  All I'm trying to say is that seems to be some race condition or some synchronization problem.</p>