2011-06-06 50 views
-1

如果一個進程是通過java runtime.exec(通過ssh)啓動的,你如何回到這個進程來殺死它(通過ssh)?有沒有辦法一旦啓動就獲得被調用的進程的PID?通過ssh發送「kill」是獲得PID的唯一途徑嗎?如何殺死一個使用java api的遠程機器進程

Process p; 

public void startSomething() 
{ 

String[] cmd = {"/usr/bin/ssh", "remoteIPAdd", "./prog"}; 

try 
{ 
    p = Runtime.getRuntime().exec(cmd); 
} 
catch(IOException e) 
{ 
    System.out.print("Epic Fail"); 
} 

} 
public void stopSomething() 
{ 

    //p.destroy if local would work but not remotely 
    //How do I stop the remote process then? 

} 

回答

0

沒有任何自動獲取遠程機器PID「prog」的方法。如果你有對prog的源代碼的控制,你可以讓它輸出PID到stdout,然後你可以從你的Process中讀取它。

假設你在一個* nix類型的機器上......一旦你有了PID,要殺死它,你將需要使用ssh發送另一個命令到遠程機器。像

String[] cmdKill = {"/usr/bin/ssh", "remoteIPAdd", "/bin/kill", ""+pid}; 

被喂入一個新的過程應該做的伎倆。