I want to detect, from inside Java, whether another process is executing.
String args = "ps -efl";
Process p = Runtime.getRuntime ().exec (args);
try
{
int exitVal = p.waitFor ();
// search output for instance of process name
BufferedReader br = new BufferedReader (new InputStreamReader (p.getInputStream ()));
String line = "";
while ((line = br.readLine ()) != null)
{
System.out.println (line);
}
}
catch (InterruptedException e)
{
System.out.println (e.getMessage ());
System.exit (0);
}
You would expect "ps -efl" to produce several dozen lines of output, at least. Instead, I get only:
PID TTY TIME CMD
21025 pts/1 00:00:00 bash
22133 pts/1 00:00:04 java
22169 pts/1 00:00:00 ps
i.e. the process id. of "ps" itself, and the Java session running it, and the bash shell running Java.
Incidentally, executing "bash -c 'ps -efl'", with or without the single quotes, produces no output.
Any idea how to list processes outside the immediate process and its parents?