JVM spawning mysterious child process of itself using Runtime.exec()
Hello, I'm not sure if this is how this is supposed to work but I have a java application that monitors legacy c programs and after a period of time (its intermittent), I'll see a duplicate jvm process running the same classpath, classname as a child of the java application monitor. This behaviour can be reproduced with the following simple class running on either solaris 9 or 10 using 1.6.0_03-b05:
public class Monitor {
Process procss;
public Monitor() {
try {
Runtime runtime = Runtime.getRuntime();
for (int i = 0; i < 10000; i++) {
System.out.println("execing command ls -l.");
procss = runtime.exec("ls -l");
procss.waitFor();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Monitor();
}
}
Using java -classpath ./ Monitor to run it. While this is running, at intermittent times doing a ps -ef you will see a duplicate jvm running whose parent process is the one that was started on the command line. Ie:
UID PID PPID etc
user 17434 10706 .... java -classpath ./ Monitor (the one I put running)
user 27771 17434 .....java -classpath ./ Monitor (intermittently started)
in another window I'll run the following shell script that will output the processes when a duplicate java process gets started as they don't seem to run very long (on my production system they will occasionally get hung up until I manually kill them):
#!/usr/bin/ksh
while ((1 == 1))
do
ps -ef | grep "Monitor" | grep -v grep > /tmp/test.out
VAL=`cat /tmp/test.out | wc -l`
if (($VAL != 1))
then
echo "Duplicate java process started"
cat /tmp/test.out
fi
done
It takes roughly 30 seconds before I start to see duplicate jvms starting to run. The concern is that is the new jvm instance running the Monitor class? Eventually on my production system the real application will have a child or 2 linger indefinetly, and threads will be deadlocked. Once I kill these child java processes, everything is back to normal. This doesn't seem to occur with the above java class but will show the duplicate child jvm's start to run after a bit.