If you use Runtime.exec() on Linux to create a process which is subsequently
killed by a signal, the exit value returned by
Process.waitFor() and
Process.exitValue() is unexpected.
Consider this code:
------------------------------------------------------------------------
public class KilledExitValue {
public static void main (String[] args) throws Exception {
Process proc = Runtime.getRuntime().exec
(new String[] { "sh", "-c", "kill -9 $$" });
System.out.println(proc.waitFor());
System.out.println(proc.exitValue());
}
}
----------------------------------------------------------------------
On Solaris, this prints:
9
9
On Linux, it prints a random value greater than 128.
Java 1.5 prints
248
248
while 1.4.2 prints
129
129
The Solaris behavior does not allow one to distinguish between a process
that did "exit (9)" and one that was killed by signal 9.
The intent on Linux is that in case of a signal, the exit value is
128 + signo, but the implementation is buggy.
The correct and expected behavior is to always return 128+signo,
which allows one to distinguish between normal and abnormal termination,
and is exactly the behavior of Unix shells:
$ sh -c 'kill -9 $$'; echo $?
Killed
137
###@###.### 2003-06-03