通过 sshj 杀死进程

发布于 2024-12-03 02:32:44 字数 962 浏览 0 评论 0原文

我正在使用 sshj 并尝试尾随文件,但我的问题是远程进程永远不会被终止。

在下面的示例代码中,您可以看到我尝试跟踪 /var/log/syslog,然后向进程发送终止信号。但是,在应用程序停止并列出服务器上的所有进程后,我仍然可以看到活动的尾部进程。

为什么这段代码不会杀死进程?我能做些什么来弥补这个问题?

    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);
            }

Im using sshj and im trying to tail a file, but my problem is that the remote process is never killed.

In the following example code you can see that i try to tail /var/log/syslog, and then i send a kill signal to the process. However after the application has stopped and i list out all the processes on the server, i can still see an active tail process.

Why will not this code kill the process? and what can i do to remedy that?

    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();
    }

EDIT

also tried sending all available signals.

            for(Signal s : Signal.values()){
                cmd.signal(s);
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小嗷兮 2024-12-10 02:32:44

分配 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 服务器都不支持它,那就没有希望了:/

Allocating a PTY and sending a Ctrl+C character code did the trick for me:

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();
}

Of course, being able to send signals would be better, but if even the OpenSSH server does not support it, there's no hope there :/

似最初 2024-12-10 02:32:44

openssh 不支持 https://bugzilla.mindrot.org/show_bug.cgi?id =1424

只需使用 cmd.close(),这也应该称为该过程

openssh doesn't support it https://bugzilla.mindrot.org/show_bug.cgi?id=1424

Just use cmd.close(), that should term the process as well

薄荷港 2024-12-10 02:32:44

这很可能是 ssh 服务器实现的问题,因为我尝试使用两个不同的 ssh 客户端并得到相同的结果。我的解决方案最终成为客户端尾部逻辑,而不是“tail -f”以防止自由漫游过程。

This is most likely a problem with the ssh server implementation, as i have tried using two different ssh clients and getting the same result. My solution ended up being a client-side tail logic, instead of "tail -f" to prevent free roaming processes.

情话难免假 2024-12-10 02:32:44

最近有类似的问题。在我的具体案例中,这是 @shikhar 提到的 OpenSSH 问题。

我的解决方案是运行启动另一个会话(共享连接)并运行终止命令 pgrep mycommand | xargs 杀死

Had a similar issue recently. In my specific case it was the OpenSSH issue mentioned by @shikhar.

My solution was to run start another session (sharing the connection) and run a kill command pgrep mycommand | xargs kill.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文