[NB. This is due to How to start a completely independent process from a Java program? but different]
I want to be able to run external processes (shell scripts) from the manager Java process, which should work when the JVM is killed, but it seems that when I kill the parent Java program, the child is also killed (note that the behavior is different if the JVM comes out of it naturally). The simplest test program I have:
public class Runit { public static void main(String args[]) throws IOException, InterruptedException { Runtime.getRuntime().exec(args[0]);
and external script:
#!/bin/sh while [ 1 ] ; do ls sleep 1 done
runs as
java -classpath jar-with-dependencies.jar temp.exec.Runit runit.sh
If the manager just exits (that is, it takes out the βwhileβ loop in the Java program), then the generated process continues to work, but when I press Ctrl + c , the Java program also kills the external program, which is not what I want.
I am using OpenJDK 1.6 on Ubuntu.
Edit1: change exec to
Runtime.getRuntime().exec("/usr/bin/nohup " + args[0]);
Does not help.
Edit2: adding a disconnect hook, as described in How to gracefully process a SIGKILL signal in Java , does not stop the spread of Ctrl + c to the child.
java
Ian rogers
source share