我正在使用Jsch跟踪服务器日志。当我关闭执行通道和会话时,“ tail -f ...”进程仍在服务器端保持活动状态。
我尝试这样做,channel.sendSignal("KILL")但抛出异常:
com.jcraft.jsch.JSchException: failed to send channel request
我该如何彻底断开连接?
小智 5
我知道这是一篇旧文章,但我正在发布我的解决方案,以防有人需要。
经过一些测试,我了解到必须发送信号的int值而不是字符串:
channel.sendSignal("2"); // CTRL + C - interrupt
channel.sendSignal("9"); // KILL
Run Code Online (Sandbox Code Playgroud)
有关更多信号,请滚动至此页面上的“标准信号” 。
我正在使用以下方法发送和中断命令。它们是此处示例的略微修改版本。
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2516 次 |
| 最近记录: |