nodejitsu / nexpect

spawn and control child processes in node.js with ease
http://github.com/nodejitsu/nexpect
Other
290 stars 37 forks source link

How to exit a program? #22

Open callumlocke opened 9 years ago

callumlocke commented 9 years ago

Some terminal programs (like file watchers) just run indefinitely until you do ctrl+c to send a SIGINT to exit the program.

How would I do this from nexpect? Is there a control character I could send through .sendline() to get the same effect as ctrl+c?

sam-github commented 9 years ago

nexpect returns the spawned child process, I can't recall exactly from which method, but look at https://github.com/nodejitsu/nexpect/blob/master/lib/nexpect.js#L298. That process supports a .kill() method.

tomek-he-him commented 9 years ago

The following works for me:

const {pid} = spawn('cat')  // This won’t end by itself.
  .expect(/* … */)
;

process.kill(pid);          // So let’s kill it!
tomek-he-him commented 9 years ago

Is there a control character I could send through .sendline() to get the same effect as ctrl+c?

I don’t think it’s possible – Ctrl + C sends the SIGINT signal and not a control character. I’ve just learned that on Stack Exchange.

But you can achieve the same as Ctrl + D (end of file/input) with nexpect’s .sendEof().