Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Destroy a process

Thread view: 
bengali - 06 Oct 2006 19:02 GMT
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)  {}
    }
}
Manish Pandit - 06 Oct 2006 19:27 GMT
>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
Nigel Wade - 09 Oct 2006 11:55 GMT
> Hi,
>
[quoted text clipped - 12 lines]
> 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).

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.