How to check if another (non-java) process is running on Linux

I am having some strange problems with this.

We use the Xvfb Virtual Desktop Manager and want it to work before I continue. Using a clean shell, I could do it easily:

ps -ef | grep Xvfb | grep -v grep 

And that gives me exactly what I need, one line containing information about Xvfb progress. Then I want to include this in my Java program and analyze the results and save the PID of the running Xvfb process. Therefore, I try:

  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 that are dumped to the console when my application starts. But if I use | grep, to narrow the list of returned processes, I get null results. input.readLine () gets null each time.

I 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 knew what I was doing wrong?

+4
source share
4 answers

Are you trying to use "|" which is a channel function, which is a feature of the shell, so you cannot do this in a java process. You can simply try to get the process id using pidof Xvfb .

+5
source

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

If you need to run commands with channels, you must explicitly call the shell:

 Runtime.getRuntime().exec(new String[] {"sh", "-c", "ps -ef | grep Xvfb | grep -v grep"}); 
+3
source

When you execute your command line directly, you are not getting a shell, and it is a shell that processes pipes. So, you would do something like "/ bin / sh -e \" ps -ef | grep Xvfb | grep -v grep \ ""

+2
source

There are many ways to do this. You can use java.lang.ProcessBuilder and "pgrep" to get the process identifier (PID) with something like: pgrep -fl java | awk {'print $1'} pgrep -fl java | awk {'print $1'} . Or, if you are running Linux, you can request the /proc directory.

I know this seems horrible, and unbearable, and even poorly implemented, I agree. But since Java actually runs in a virtual machine, for some absurd reason that I can’t understand after more than 15 years of working with the JDK, why it is impossible to see things outside the JVM space, this is really funny if you think about it. You can do the rest, even develop and join child processes (it was a terrible way to multitask when the world did not know about threads or pthreads , what the hell! What happens with Java ?! :).

This will give a huge discussion that I know, but in any case there is a very good API that I have already used in my projects, and it is quite stable (this is OSS, so you still need to emphasize each version you use before trusting the API) : https://github.com/jezhumble/javasysmon

JavaDoc: http://jezhumble.imtqy.com/javasysmon/ , find the class com.jezhumble.javasysmon.OsProcess , it will do the trick. Hope this helps, good luck.

0
source

Source: https://habr.com/ru/post/1385214/


All Articles