我正在使用sshj,并试图跟踪一个文件,但我的问题是远程进程永远不会被终止。
在下面的示例代码中,您可以看到我尝试结尾/var/log/syslog,然后我向进程发送一个kill信号。但是,在应用程序停止后,我列出了服务器上的所有进程,我仍然可以看到一个活动的尾部进程。
为什么这段代码不会终止进程?我能做些什么来弥补这一点?
SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(new PromiscuousVerifier());
try {
ssh.connect("localhost");
ssh.authPassword("xxx", "xxx");
final Session session = ssh.startSession();
try {
final Command cmd = session.exec("tail -f /var/log/syslog");
cmd.signal(Signal.KILL);
System.out.println("\n** exit status: " + cmd.getExitStatus());
} catch (IOException e) {
e.printStackTrace();
}finally{
session.close();
}
} finally{
ssh.disconnect();
}编辑
也尝试发送所有可用的信号。
for(Signal s : Signal.values()){
cmd.signal(s);
}发布于 2011-09-09 22:03:56
这很可能是ssh服务器实现的问题,因为我尝试使用两个不同的ssh客户机,并获得相同的结果。我的解决方案最终是客户端的尾部逻辑,而不是“尾部-f”,以防止自由漫游过程。
发布于 2013-11-15 02:49:09
为我分配一个PTY并发送一个Ctrl+C字符代码就可以了:
final Session session = ssh.startSession();
session.allocateDefaultPTY();
try {
final Command cmd = session.exec("tail -f /var/log/syslog");
// Send Ctrl+C (character code is 0x03):
cmd.getOutputStream().write(3);
cmd.getOutputStream().flush();
// Wait some time for the process to exit:
cmd.join(1, TimeUnit.SECONDS);
// If no exception has been raised yet, then the process has exited
// (but the exit status can still be null if the process has been killed).
System.out.println("\n** exit status: " + cmd.getExitStatus());
} catch (IOException e) {
e.printStackTrace();
}finally{
session.close();
}当然,能够发送信号会更好,但如果连OpenSSH服务器都不支持它,那就没有希望了:/
发布于 2011-09-08 04:08:27
openssh不支持它https://bugzilla.mindrot.org/show_bug.cgi?id=1424
只需使用cmd.close(),这也应该用术语表示这个过程
https://stackoverflow.com/questions/7330692
复制相似问题