Skip to content

Close Command Execution Not Working #422

Closed

Description

@dudu631

Hi,

I'm running a command using execCommand method. Also I'm passing a onChannel ( clientChannel: ClientChannel) option as parameter to get client channel connection.

My issue is that I can't by any mean stop the command execution (simulating a CTRL-C during an execution).

I've tried:

clientChannel.close()
clientChannel.end()
clientChannel.write('\x03')
clientChannel.signal('INT')

To test this out, I run a command with execCommand then I close it, and after I check my running processes on my server it's still running there. I have to mannually kill them with kill {pid}.

What am I missing?

Activity

  1. dudu631 commented on Mar 1, 2022

    @dudu631
    Author

    One thing I just found out:

    The command I'm using invokes an executable file.
    Example:
    ./executable arg1 arg2

    A command without an executable file like ping, the close option works.

    Is there a way to kill the process of a executable file command, without grep and kill?

  2. dudu631 commented on Mar 1, 2022

    @dudu631
    Author

    Currently using a workaround:

    Passing execOptions: { pty: true } to execCommand params seems to make clientChannel.close() work.

    Hoping to listen to more ideas on this.

  3. steelbrain commented on Nov 16, 2026

    @steelbrain
    Owner

    Sorry for the very late reply. I tracked down why none of those worked:

    • close()/end(): without a PTY, OpenSSH leaves the command running when the channel closes. With pty: true, closing hangs up the terminal, which sends SIGHUP; that's why your workaround works. ping stopped anyway because it keeps writing output and dies with SIGPIPE once nobody reads it.
    • write('\x03'): without a PTY, Ctrl-C is just a byte on the command's stdin.
    • signal('INT'): ssh2 only sends signal requests while the channel's stdin is open, and execCommand closes stdin right away when you don't pass stdin. So nothing was sent.

    v14 adds timeout and signal (an AbortSignal) options. They send the signal request properly, then close the channel:

    const controller = new AbortController()
    const running = ssh.execCommand('./executable arg1 arg2', { signal: controller.signal, killSignal: 'INT' })
    // later
    controller.abort()

    Against OpenSSH 9.2, this stopped sleep 100, a while true shell loop and sh -c 'sleep 100; ...', including their child processes (sshd signals the process group). The default killSignal is TERM; use KILL for programs that ignore it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions