Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - how to check whether another (non-Java) process is running on Linux

I'm having some weird problems with this.

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:

    ps -ef | grep Xvfb | grep -v grep

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:

    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);
    }

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.

I have also tried:

    ps -ef | grep Xvfb | grep -v grep | awk {'print $2'}

To just grab the process id. Also, no luck.

Has anyone else experienced this or know what I'm doing wrong?

like image 342
AWT Avatar asked Dec 07 '11 21:12

AWT


People also ask

How do you check if a process is running in Linux using Java?

On Linux, you can view processes with the ps command. It is the simplest way to view the running processes on your system. You can use the ps command to view running Java processes on a system also by piping output to grep .

How do you check if a file is used by another process in Java?

You can run from Java program the lsof Unix utility that tells you which process is using a file, then analyse its output. To run a program from Java code, use, for example, Runtime , Process , ProcessBuilder classes.

Which command is used to know whether Java is running in Linux machine or not?

Use the commands: sudo yum list installed and sudo yum list installed | grep -i openjdk instead. With this article, you have successfully checked the Java version installed on Linux.

How can I see what programs are running on Linux?

Open the terminal window on Linux. For remote Linux server use the ssh command for log in purpose. Type the ps aux to see all running process in Linux. Alternatively, you can issue the top command or htop command to view running process in Linux.


2 Answers

You're trying to use the "|" which is a pipe function that is particular to the shell, therefore you cannot do it in the java process. You could just try getting the process ID by using pidof Xvfb.

like image 188
Max Avatar answered Sep 21 '22 10:09

Max


Maybe Runtime.getRuntime().exec() tries to execute the program as it is in the argument. That is, it runs the program ps with arguments -ef, |, grep, etc. And so, the program fails because it does not understand what's going on.

If you need to run piped commands, you should call the shell explicitly:

Runtime.getRuntime().exec(new String[] {"sh", "-c", "ps -ef | grep Xvfb | grep -v grep"});
like image 44
rodrigo Avatar answered Sep 19 '22 10:09

rodrigo