title: Java - how to check whether another (non-Java) process is running on Linux
body: <p>I'm having some weird problems with this.</p>
  
  <p>We are using Xvfb virtual desktop manager and want to make sure it's running before I continue.  Using pure shell, I could do this easily:</p>
  
  <pre><code>    ps -ef | grep Xvfb | grep -v grep
  </code></pre>
  
  <p>And that gives me exactly what I need, a single line containing information about the Xvfb proc. Next, I want to incorporate this into my Java program and parse the results and store the PID of the running Xvfb process.  So I am trying this:</p>
  
  <pre><code>    String line;
      try {
        Process p = Runtime.getRuntime().exec("ps -ef | grep Xvfb | grep -v grep");
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while ((line = input.readLine()) != null)
        {
          System.out.println(line);
        }
      } catch (Exception err) {
        System.out.println(err);
      }
  </code></pre>
  
  <p>The bizarre thing is that if I use "ps -ef", I get a huge list of processes dumped to my console when I run my app.  But if I use | grep to narrow the list of processes returned, I get zero results.  input.readLine() gets null every time.</p>
  
  <p>I have also tried:</p>
  
  <pre><code>    ps -ef | grep Xvfb | grep -v grep | awk {'print $2'}
  </code></pre>
  
  <p>To just grab the process id.  Also, no luck.</p>
  
  <p>Has anyone else experienced this or know what I'm doing wrong?</p>