jsch 中执行 ctrl+c, ctrl+x

 在 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);

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值