• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Running "top -b | grep idle " from a JAVA program

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We are trying to execute following four commands from within a Java Program.
But only first three are running i.e. 1, 2 and 3.
Can someone explain why the 4th command is not running and what will be the correct way?
1. Runtime.getRuntime().exec(new String[] {"/bin/sh","-c", "ls / 2>&1 | tee ls.log"});
2. Runtime.getRuntime().exec(new String[] {"/bin/sh","-c", "top -b 2>&1 | tee top.log"});
3. Runtime.getRuntime().exec(new String[] {"/bin/sh","-c", "ls / 2>&1 | grep auto"});
4. Runtime.getRuntime().exec(new String[] {"/bin/sh","-c", "top -b 2>&1 | grep idle"});
Thanks and sincere regards,
Sangeeta
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
two things:
1. "Sangeeta S",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
2. This forum is for discussion of this web site. For general Java questions you should really ask in one of the specific forums. In this case I have moved your post to the "Unix/Linux" forum, which seems the most appropriate.
Thanks.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sangeeta,
I am not sure but here are a few things.
The way you are running the top command with -b option means that it will execute until it is killed. It is possible that the current thread is not waiting for the process to execute.
You might try something like -
Process p = Runtime.getRuntime().exec(new String[] {"/bin/sh","-c", "top -b 2>&1 | grep idle"});
p.waitFor(); //ask current thread to wait for this process to finish
Of course you will have to modify your top command and give it the number of iterations to run with the -n flag:
top -b -n 10.....
Hope this helps.
Best wishes,
Amol
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic