开发者

How do I kill a remote machines process using java api

If a process is started through java runtime.exec (via ssh) how do you get back to this process to kill it (via ssh)? Is there a way to obtain the PID of the invoked process once started? Is sending "kill" via ssh the only way if a PID is obtainable?

Process p;

public void startSomething()
{

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

 try
 {
   p =开发者_JAVA技巧 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?

}


There isn't any automatic way to get the remote machine's PID for "prog". If you have control of prog's source, you could have it output its PID to stdout and then you could read it from your Process.

Assuming you're on a *nix type machine... Once you have the PID, to kill it you will need to send another command to the remote machine using ssh. Something like

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

being fed into a new process should do the trick.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜