Problem to get output of a Linux process
843798Apr 18 2006 — edited Apr 19 2006Hello,
I know that this topic has been discussed a lot of times in this forum but I read all of them and I cannot solve my problem.
I created a class which is launching a small linux script used to extract the PID of a specific application.
My problem is that I cannot get the process output, then I cannot get the PID :(
Here is the code I wrote:
-------------------------------------------------
public String getPID(String szProcessName) {
BufferedReader input = null;
String szReturnLine = "";
try {
myProcess = myRunTime.exec( public String getPID(String szProcessName) {
BufferedReader input = null;
String szReturnLine = "";
try {
myProcess = myRunTime.exec("ps axw | grep -m 1 \""+ szProcessName + "\" | grep -v \"grep\" | awk '{print $1}'");
//read result
InputStreamReader isReader = new InputStreamReader(myProcess.getInputStream());
input = new BufferedReader(isReader);
isReader.read();
//get the output from the process
Date dtBegin = new Date();
long lTimeout = 0;
do {
while(input.ready()){
szReturnLine = input.readLine();
}
//szReturnLine = input.readLine();
if (log.isDebugEnabled()) {
log.debug("GET PID of " + szProcessName);
}
Date dtNow = new Date();
lTimeout = dtNow.getTime() - dtBegin.getTime();
if (log.isDebugEnabled()) {
log.debug("Timeout: " + lTimeout);
}
try {
Thread.sleep(10); //a little bit more speed
} catch (InterruptedException eip) {
}
} while ((szReturnLine != null) && (lTimeout < this.lExecTimeout));
} catch (Exception ex) {
log.error("Exception occured in 'getPID' function : " + ex);
return "";
}
return szReturnLine;
}
---------------------------------------------
My problem is that I cannot read the output of the process returning the PID.
I don't know if it can be because the process is too fast or if my code is wrong.
Thanks in advance for your help.
Philipina.