在 jsch 中使用 ChannelExec 时候, 可能会遇到命令的阻塞需要手动执行 ctrl+c 命令
其实ctrl + * 这些只是信号量, 可以直接对标准输出流发送对应的对应的 信号 + \n 就可以了
如: ctrl + c 的信号是 3, ctrl + d 的信号是 4, ctrl + x 的信号是 24
// 这几行是我是写到构造方法里的
// 分配伪终端 当程序关闭时 命令进程一起关闭
// channel.setPty(true);
// this.outputStream = channel.getOutputStream();
/**
* 写入命令
*
* @param command command
* @param lf 是否键入 \n
* @return this
*/
public BaseRemoteExecutor write(byte[] command, boolean lf) {
try {
outputStream.write(command);
if (lf) {
outputStream.write(Letters.LF);
}
outputStream.flush();
} catch (IOException e) {
throw Exceptions.ioRuntime(e);
}
return this;
}
/**
* 中断 键入 ctrl+c
*
* @return this
*/
public BaseRemoteExecutor interrupt() {
return this.write(new byte[]{3}, true);
}
/**
* 挂起 键入 ctrl+x
*
* @return this
*/
public BaseRemoteExecutor hangUp() {
return this.write(new byte[]{24}, true);
}
或者执行 echo -e '\003' 应该也是可以的
如果想直接结束命令, 可以直接将标准输入,流标准输出流 close 掉命令也就直接停止了, 前提是ChannelExec 设置是伪终端, channel.setPty(true);