Destroy a process
Hi,
I have to create processes within a Java class on Solaris 8->10 and I
noticed that I was unable to kill the process I created.
To test, i created a sample test.sh file that just does a sleep 1000000.
I call it from my Java class.
The problem is that when my Jvm exits, the process of the shell script
is detached from the process of my Jvm but still runs. Even if i
explicitly call
the destroy() method on my process reference.
I tried registering a JVM shutdowhook like below but it doesn't have
any effect.
Do you know anyworkaround ? How would you force killing processes
launched from a Jvm ?
Thanks in advance,
Luke
public class Test2 {
public static void main(String[] args) throws Throwable {
Test2 t = new Test2();
t.test();
System.exit(1);
}
public void test() throws Throwable {
System.out.println("Time to see if process is still running");
Thread t = new Thread(new T());
t.start();
t.join(4000);
}
}
class T implements Runnable {
public void run() {
try {
System.out.println("Starting process");
final Process p = Runtime.getRuntime
().exec("/bin/sh /tmp/java/test.sh");
Runtime.getRuntime().addShutdownHook(
new Thread(){
public void run(){
p.destroy();
}
}
);
try
{
p.waitFor();
}
catch (InterruptedException e) { /* Ignore */ }
} catch (Throwable t) {}
}
}