Destroy a process

B

bengali

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) {}
}
}
 
M

Manish Pandit

From the javadocs:

"The Runtime.exec methods may not work well for special processes on
certain native platforms, such as native windowing processes, daemon
processes, Win16/DOS processes on Microsoft Windows, or shell scripts."

Looks like your case hit the boundary.

-cheers,
Manish
 
N

Nigel Wade

bengali said:
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 ?

What state is the resulting process left in; is it a zombie process?

If so, that means that the process hasn't been "reaped", i.e its exit status is
still being held by the kernel so it can be returned, but the parent process
isn't waiting for it to exit. The process doesn't really exist, it has
terminated and gone away. All that remains is the "zombie", an entry in the
kernel process table.

You can probably get around this situation by calling Process.exitValue() after
Process.destroy(), which should reap the process exit status and allow it to
disappear completely. (This is speculation on my part... I haven't tried it,
but it's what I would try were I to encounter this situation).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top