Ubuntu Java: How to run Linux shell scipt from Java program and fetch back its output

Hi: I am using Java (HTTPUnit) in Linux machine. I am using HTTPUnit multi thread to visit a web application (Hibernate, Spring, tomcat, mysql). Experts in this forum give me the script to see Tomcat JVM memory consumption. Now when I use HTTPUnit to visit a page, then I want to check memory usage by the program themself. And see how much memory consumption it takes. If the memory consumption is bigger, I could write it to some file. My question is from Java program how to run a linux script and how to get output back.

The script is as follows: It could be stored in some executable file called checkTomcatJVM.sh.

ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | grep java
JianJunShenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ksivananthCommented:
you can use the Runtime class from java to get the total memory and freememory!

check Runtime.freeMemory()
Runtime.totalMemory
0
jwentingCommented:
Use ProcessBuilder to execute the external process.
Wait for the process to complete, and capture its output stream.
0
flob9Commented:
Runtime.getRuntime().exec(new String[] {"script","param"});
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

JianJunShenAuthor Commented:
Runtime.getRuntime().exec(new String[] {"script","param"});

This sounds a good solution to run script. But how to fetch output?
0
flob9Commented:
Something like this :

Process proc = Runtime.getRuntime().exec(new String[] {"script","param"});

byte[] buff = new byte[1024];

InputStream out = proc.getInputStream();
int nb = out.read(buff);


But, as mentionned before, calling external process for this purpose is quite dirty.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JianJunShenAuthor Commented:
Oh; sorry , I push the wrong button. Now the excellent answer would be closed. My mistake.
0
JianJunShenAuthor Commented:
Excellent answer and very fast.
0
Mick BarryJava DeveloperCommented:
you need to read in a loop to ensure you get all output written to stdout


http://helpdesk.objects.com.au/java/runtime-exec-locks-up
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.