5

I am using Jsch to tail a server-log. When I close my exec-channel and session, the "tail -f ..." process still stays alive at server side.

I tried to do channel.sendSignal("KILL") but it throws an exception: com.jcraft.jsch.JSchException: failed to send channel request

how can I do a clean disconnect?

wrm
  • 1,898
  • 13
  • 24

2 Answers2

5

I know this is an old post but I'm posting my solution in case someone needs it.

After some testing I learned that I had to send the int value of the signal instead of the string:

channel.sendSignal("2"); // CTRL + C - interrupt
channel.sendSignal("9"); // KILL

For more signals scroll to 'Standard signals' on this page.

I'm using the following methods to send and interrupt commands. They are slightly modified versions of the example found here.

public String sendCommand(String command)
{
  StringBuilder outputBuffer = new StringBuilder();

  try
  {
    Channel channel = sesConnection.openChannel("exec");
    ((ChannelExec)channel).setCommand(command);
    channel.connect();
    InputStream commandOutput = channel.getInputStream();

    int readByte = commandOutput.read();
    while(readByte != 0xffffffff)
    {
      outputBuffer.append((char)readByte);
      readByte = commandOutput.read();
      if (interrupt)
      {
        interruptChannel(channel);
        break;
      }
    }

    channel.disconnect();
  }
  catch(IOException ioX)
  {
    logWarning(ioX.getMessage());
    outputBuffer.append(ioX.getMessage());
    return null;
  }
  catch(JSchException jschX)
  {
    logWarning(jschX.getMessage());
    outputBuffer.append(jschX.getMessage());
  }
  finally
  {
    setInterrupt(false);
  }

  return outputBuffer.toString();
}

private void interruptChannel(Channel _channel)
{
  try
  {
    _channel.sendSignal("2");
  }
  catch (Exception e)
  {
    logger.error("Failed interrupting channel", e);
  }
}
Community
  • 1
  • 1
papkass
  • 1,261
  • 2
  • 14
  • 20
  • sadly, it throws the same exception. but if this works for you, the mistake seems to be on my side... anyway, i will close this question. last remark: even documentation says to send "Signal without SIG prefix" (see here: http://epaul.github.io/jsch-documentation/simple.javadoc/com/jcraft/jsch/Channel.html). – wrm Oct 23 '14 at 11:43
  • I got an exception when sending "INT" but with "2" it worked. I have updated my post with the methods I'm using to send commands and signals. – papkass Oct 23 '14 at 12:22
0

Update: with implementation 'com.jcraft:jsch:0.1.55' on a Dropbear ssh server, channel.sendSignal("KILL") worked, while channel.sendSignal("9") did absolutely nothing.

Luca Fabbian
  • 194
  • 6