Killing processes from Java

Tags:

Answer: 1

1 hours ago

I have created a Java application that can start processes and capture their PIDs so that the application can monitor the process. The user may later choose to kill the whole process tree (session). I have done this by utilizing setsid. One scenario is starting a whole bunch of different processes via a shell script by: setsid <shell script>. The PID represents the new session and killing it will kill all processes spawned in that session. It worked great until Ubuntu 12.10 was released. Now i cannot kill all sessions started via setsid. One example is Firefox and Google Earth. Gedit can still be killed if started through setsid.

I created a simple test program that perform kill -SIGTERM -PID. The test program manages to kill firefox started via setsid under Ubuntu 12.04 but not under Ubuntu 12.10. I don't know what has changed. I have executed the program in both distributions using OpenJDK 6, 7 and Oracle JDK 6.


public class kill
{
        public static void main(String[] args)
        {
    try 
        {
        System.out.println("kill -SIGTERM -" + args[0]);
        Process proc = Runtime.getRuntime().exec("kill -SIGTERM -" + args[0]);
        int exitVal = proc.waitFor();
        System.out.println("Exit value: " + exitVal); // often 1 under Ubuntu 12.10
        }
    catch (Exception e)
        {
        }
}

Added by: Prof. Shane Klein

Answer: 2

12 hours ago

In Ubunutu 12.04 and Centos 5 + 6 it's possible to use the signal name like SIGTERM but on 12.10 one has to use -TERM instead. Changing my code so that the signal names don't have the prefix 'SIG' seem to work well on 12.04 and 12.10. So the example code that works for me is:

Process proc = Runtime.getRuntime().exec("kill -TERM -" + args[0]);

To clarify, kill -SIGTERM -<PID> works in the Bash shell in Ubuntu 12.10 but not if it's executed through Runtime.getRuntime.exec(), one qould have to remove the 'SIG' part as described above.

Added by: Dr. Clint Thompson

Popular Search

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9