*
The moose likes Java in General and the fly likes how to list process of linux in Java application? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to list process of linux in Java application?" Watch "how to list process of linux in Java application?" New topic
Author

how to list process of linux in Java application?

Ritesh Dwa
Greenhorn

Joined: Sep 18, 2012
Posts: 6
Dear All,
I am using jdk1.6 to develop an application which should list only particular processes running in a RHEL 5 server. I had used 'Runtime.getRuntime().exec("ps -ef|grep java")' to call linux process but does not work. It works if I use "ps -ef" as command.

Can anyone suggest me, how to run joined command of linux through application.

Thank you.


Best Regards,
Ritesh
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 866
    
    5
"does not work" is not very informative.
Exec'ing can be tricky with the handling of the output streams.

| (pipe) and redirections (<,>, etc.) are handled by the way by the shell, so you might need something like this


Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

and you need to read all the 4 sections of http://www.javaworld.com/jw-12-2000/jw-1229-traps.html and implement ALL the recommendations.

P.S. Since you are using JDK1.6 you can use ProcessBuilder rather than Runtime.exec(). Runtime.exec() uses ProcessBuilder behind the scenes but has an improved API compared to Runtime.exec(). The 'traps' article still apples to Process objects created using ProcessBuilder.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

Ritesh Dwa wrote:It works if I use "ps -ef" as command.

OK, so use that. I presume you're familiar with String.matches().

However, on a general note, Java was never designed for running system commands; indeed, it runs contrary to the whole ethos of Java - portability.
Your solution, for example, will only work on a Unix/Linux system.

On the other hand, it should be a very simple matter to write a script that runs the pipeline you want and sends the output of that to a Java program.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ritesh Dwa
Greenhorn

Joined: Sep 18, 2012
Posts: 6
Thank you all for your suggestion.

I have implemented the suggestion and now the application can handle the command smoothly.

Best Regards,
Ritesh
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to list process of linux in Java application?
 
Similar Threads
problem to starting server
How to ensure Weblogic is running or not with Linux command
Connection refuse on tomcat
Identifying Java Processes on UNIX boxes
How to kill rmiregistry on Linux?