I am making some monitoring thread. Have stored the process IDs (PID) in a hash map
. In thread I am retrieving those PID and on certain condition want to kill particular PID. I searched on web, for windows
environment the kill command was : Runtime.getRuntime().exec("taskkill /F /PID "+PID_id);
where PID_id is the PID I get from map. I want to know, how can I do such thing in ubuntu
(Linux
) environment.
Asked
Active
Viewed 567 times
0

JPG
- 1,247
- 5
- 31
- 64
-
You use the `kill` command; read all about it in `man kill` – Stephen C Dec 02 '15 at 13:24
-
Why are you doing that in Java instead of relying on existing OS utilities to handle such stuff? – fge Dec 02 '15 at 13:27
-
check this > http://stackoverflow.com/questions/7762257/ubuntu-java-find-a-specific-programs-pid-and-kill-the-program – newuserua_ext Dec 02 '15 at 13:28
1 Answers
1
Process process = Runtime.getRuntime().exec("kill -9 "+PID_id);
int retCode = process.waitFor();

Saril Sudhakaran
- 1,109
- 9
- 17
-
if you want return code , the java program has to wait for the proces to complete and do a process.waitFor() – Saril Sudhakaran Dec 02 '15 at 13:31
-
@sarilsudhakaran what will happen if I don't use `int retCode = process.waitFor();`. I want process with PID_id immediately killed. Don't want to wait for its completion. – JPG Dec 03 '15 at 04:39
-
waitFor() is called to make sure that its killed and check the return code . you can ignore that call and continue processing – Saril Sudhakaran Dec 07 '15 at 10:17