0

I need the ability to kill linux process using java. I have used the commands below but it appears i don't have the permissions to kill the process even though the process is of the same user. I have also tried running the application with sudo ie. "sudo java -jar appname.jar"

    private static void forceKillRunningProcess() throws Exception{
    //Runtime.getRuntime().exec("sudo pkill -f 'application.jar'");
    ProcessBuilder pb = new ProcessBuilder("sudo","pkill","-f", "\"application.jar\"");
    pb.start();

}

the commands above work if in CLI but not in java. Is there a work around?

Salim
  • 199
  • 3
  • 18
  • if it's all running as the same user, why would you need sudo? a user can kill their own processes without having to resort to root privs. – Marc B Apr 13 '15 at 16:34
  • 1
    What is the exit value ? – ortis Apr 13 '15 at 16:35
  • are you getting any error or exception? you should run that in a try block. can you try to capture the system dump? – Nirmal Apr 13 '15 at 16:36
  • @MarcB that's what I was thinking. But i think the pkill requires root priv. but I'm not sure. user3320018 i'm going to use a try catch now and see what is returned. I should also mention that when i run the command without sudo in CLI it returns "operation not permitted" – Salim Apr 13 '15 at 16:42
  • There is a related post on SO: [Running command in new terminal][1] [1]: http://stackoverflow.com/questions/15714870/java-starting-running-command-in-new-terminal-mac-osx-process-builder-not-ru – Nirmal Apr 13 '15 at 16:52

2 Answers2

1

If the process is "external" (which wasn't started from your Java application - ie : by a ProcessBuilder.start()) then you can't kill it with that command.

On the other hand, you could try a : Runtime.exec("pkill -f 'application.jar'"); but your Java application would only run on a Unix environment having the pkill binary - thus not be platform independent.

Juergen
  • 12,378
  • 7
  • 39
  • 55
Azzip
  • 122
  • 10
  • the purpose of the command is to kill the process if it hangs. how can I kill a process in linux using java? I wouldn't be able to have it application kill itself if it hangs. – Salim Apr 13 '15 at 16:48
  • 2 options: 1/ you start the JAR from your Java application and keep a reference to it in order to stop it later one (Process jarToStop = new .... [...] jarToStop.destroy(); ) 2/ if the former is not possible, you can execute a Runtime command just as I told you before - but that usually isn't recommanded since a Java application is "supposed" to be platform independant – Azzip Apr 13 '15 at 16:54
1

I guess the problem is with sudo - usually when we try such command from terminal, it'll immediately prompt us to enter the password, which is not happening in the code posted. Below code works, but if I add sudo in kill statement, it doesnt.

package com.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class KillApp {

public static void main(String[] args) {

    try {
        Process pr = Runtime.getRuntime().exec("pidof chrome");
        BufferedReader read = new BufferedReader(new InputStreamReader(
                pr.getInputStream()));
        String str = read.readLine();
        System.out.println("Process id(s) to kill: " + str);
        pr = Runtime.getRuntime().exec("kill -9 " + str);
        System.out.println("done");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}
Pavan Kumar
  • 4,182
  • 1
  • 30
  • 45
  • Good to know that, but I would recommend to explore on how to handle it in a multi user environment. It worked for us because probably we both work on a single user environment or logged in with "root" user. – Pavan Kumar Apr 13 '15 at 18:12