Dear Friends,
I have to execute the below unix command through java program.
*ps -ewwo pid,args | grep -E "abc.sh|xyz.jar" | gawk '{ print $1 }' | wc -l*
My code to execute this command is,
Runtime run = Runtime.getRuntime();
File workDir = new File("/root/sat");
String psCmd = "ps -ewwo pid,args | grep -E \"abc.sh|xyz.jar \" | gawk '{ print $1 }' | wc -l";
Process process = run.exec(psCmd, null, workDir);
String line;
int i = process.waitFor() ;
if(i == 0) {
BufferedReader buf = new BufferedReader( new InputStreamReader( process.getInputStream() ) ) ;
while ( ( line = buf.readLine() ) != null )
{
System.out.println("Line - ["+line+"]") ;
}
} else {
BufferedReader stdErr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// read the output from the command
while ((line = stdErr.readLine()) != null) {
System.out.println(line);
}
}
When i execute this command, i'm getting output as,
ERROR: Garbage option.
When i analyse the above error, i found that, the
PIPE ( | ) command is not supported through java.
Could anyone please help me how to execute the above unix command (with | ) in java?
Thanks in advance,
Sathish