You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
lettimerID=setTimeout(()=>{console.log('sorry the timeout')},5000)ssh.execCommand('echo hello',{cwd:'/'}).then((result)=>{console.log('nice, we got peer command output'+result.stdout)}).catch((err)=>{console.log('something error')}).finally(()=>{clearTimeout(timerID)timerID=null})
Thanks for writing in. Since this library is a wrapper around the ssh2 package, it supports the same features.
If you are using the exec or execCommand functions, it's not possible (as per my current knowledge) to issue an abort command. But depending on your usecase, you should be able to use the requestShell function, and invoke the command in the shell. Inside the shell, you can send various signals like ctrl-c through stdin.
If the commands you are executing are non consequential, you can do what @zydingjun recommended above and have a timeout in your local code
Thanks for asking, this is now built in. v14 adds three options to exec and execCommand:
// Rejects with an SSHError whose code is 'ETIMEDOUT' after 5 secondsawaitssh.execCommand('./long-task.sh',{timeout: 5000})// Stop it yourself with an AbortController; rejects with the abort reasonconstcontroller=newAbortController()construnning=ssh.execCommand('tail -f /var/log/syslog',{signal: controller.signal,onStdout: (chunk)=>process.stdout.write(chunk),})setTimeout(()=>controller.abort(),10000)
When the timeout passes or the signal aborts, node-ssh asks the server to send killSignal (default 'TERM') to the command and closes the channel. Against OpenSSH 9.2, the command and its child processes all exited right away (sshd signals the whole process group). Two limits:
A command that ignores TERM keeps running; use killSignal: 'KILL' for those.
OpenSSH refuses to signal sessions running a forced command from authorized_keys.
Activity
zydingjun commentedon Jul 27, 2023
these demo may help you:
Edit by @steelbrain: Added formatting
steelbrain commentedon Aug 7, 2023
Hi @sga8!
Thanks for writing in. Since this library is a wrapper around the ssh2 package, it supports the same features.
If you are using the
execorexecCommandfunctions, it's not possible (as per my current knowledge) to issue an abort command. But depending on your usecase, you should be able to use therequestShellfunction, and invoke the command in the shell. Inside the shell, you can send various signals like ctrl-c through stdin.If the commands you are executing are non consequential, you can do what @zydingjun recommended above and have a timeout in your local code
steelbrain commentedon Nov 16, 2026
Thanks for asking, this is now built in. v14 adds three options to
execandexecCommand:When the timeout passes or the signal aborts, node-ssh asks the server to send
killSignal(default'TERM') to the command and closes the channel. Against OpenSSH 9.2, the command and its child processes all exited right away (sshd signals the whole process group). Two limits:killSignal: 'KILL'for those.authorized_keys.Add timeout, signal and killSignal options to exec and execCommand (#465
Don't count zombies as running in the process-group test helper