通过 sshj 杀死进程 [英] Killing a process through sshj

查看:64
本文介绍了通过 sshj 杀死进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 sshj 并且我试图拖尾文件,但我的问题是远程进程永远不会被杀死.

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

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

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

编辑

还尝试发送所有可用信号.

also tried sending all available signals.

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

推荐答案

这很可能是 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.

这篇关于通过 sshj 杀死进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆