1

I'm trying to execute the following Command in Java in order to kill the spawned process of bash script which is executed through java :

kill $(pgrep -P $(pgrep -P 5537)) 

I'm using apache Commons Exec Commandline to build the Command but it's no different to using ProcessBuilder here. So here is what I have so far:

CommandLine cmdLine = new CommandLine("bash");
cmdLine.addArgument("-c");
cmdLine.addArgument("kill $(pgrep -P $(pgrep -P "+pid+"))");

I get the error

bash: $'kill 7940\n7941\n7942\n7943': Command not found.

Normally I would now try to get the newlines out of the Command but it also doesn't work to kill just a single process because then I get the error :

bash: kill 7980: Command not found.

One the one hand I need to use bash to use the variables and on the other hand I can't use it because kill can't be executed with it...

1
  • How are you going to programmatically fetch the pid and give it to a Java file? That's the first thing to solve. Commented Oct 19, 2012 at 9:10

1 Answer 1

1

firstly kill -9 pidnumber

Why would you need the bash variables? when java gives you strings to store variables?

Thirdly why not try System.Runtime.getRuntime().exec() ?

Also do you have permissions to kill the task? tried sudo kill -9 pid?

Sign up to request clarification or add additional context in comments.

1 Comment

why use java variables when I can use bash variables? I would have to execute runtime.exec serveral times if I used java variables. Anyway Runtime.getRuntime().exec(new String[]{"bash","-c","kill $(pgrep -P $(pgrep -P "+pid+"))"}); did it for me thanks ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.