Spawning A Child Process With Tty In Node.js
Solution 1:
There's a few options:
If you don't mind re-using stdin/stdout/stderr of the parent process (assuming it has access to a real tty) for your child process, you can use
stdio: 'inherit'
(or onlyinherit
individual streams if you want) in yourspawn()
options.Create and use a pseudo-tty via the
pty
module. This allows you to create a "fake" tty that allows you to run programs likesudo
without actually hooking them up to a real tty. The benefit to this is that you can programmatically control/access stdin/stdout/stderr.Use an ssh module like
ssh2
that doesn't involve child processes at all (and has greater flexibility). Withssh2
you can simply pass thepty: true
option toexec()
andsudo
will work just fine.
Solution 2:
ssh -qt user@remote.machine -- "sudo mv ./this.thing /to/here/;"
Per the ssh man page:
-t Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
When you run ssh
interactively, it has a local tty, so the "-t" option causes it to allocate a remote tty. But when you run ssh
within this script, it doesn't have a local tty. The single "-t" causes it to skip allocating a remote tty. Specify "-t" twice, and it should allocate a remote tty:
ssh -qtt user@remote.machine -- "sudo mv ./this.thing /to/here/;"^^-- Note
Solution 3:
Some programs must be running tty. "child_process" library working bad when tty inputs. I have tried for docker ( docker run command like ssh need tty), with microsoft node-pty library https://github.com/microsoft/node-pty
And this is worked me.
let command = "ssh"let args = '-qt user@remote.machine -- "sudo mv ./this.thing /to/here/;"'const pty = require("node-pty");
let ssh = pty.spawn( command , args.split(" ") )
ssh.on('data', (e)=>console.log(e));
ssh.write("any_input_on_runngin_this_program");
Post a Comment for "Spawning A Child Process With Tty In Node.js"