Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Killing a process through sshj

Tags:

java

ssh

sshj

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);
            }
like image 880
netbrain Avatar asked Sep 07 '11 08:09

netbrain


People also ask

How do you kill a task in PID?

To kill the process using PID a) Type the following command into the command prompt, to kill only one Process, and press Enter Key. For Example - To kill Notepad, run the command as, taskkill /PID 2404 /F, where /F is used to kill the process forcefully.


3 Answers

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 :/

like image 106
Tey' Avatar answered Sep 22 '22 03:09

Tey'


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

like image 38
shikhar Avatar answered Sep 23 '22 03:09

shikhar


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.

like image 25
netbrain Avatar answered Sep 22 '22 03:09

netbrain