How can I run an interactive shell command?

3,326 views
Skip to first unread message

Stuart Rackham

unread,
Nov 9, 2011, 10:39:43 PM11/9/11
to nodejs
Hi

I want to run an interactive console shell command; wait for the user
to exit;
then continue i.e. the same as if you were running it manually from
the command-line. I just can't figure out how to do it.

This worked in node v0.4.12, but customFd is now deprecated and
it doesn't work in v0.6.0:

pager = spawn('less', ['Cakefile'], {
customFds: [process.stdin, process.stdout, process.stderr]
});

So I tried an interactive shell (node v0.6.0):

pager = spawn('/bin/bash', ['-i', '-c', 'less', 'Cakefile']);

Still doesn't work.

My platform is Xubuntu 10.04 (32bit), node v0.6.0 built from source.

Any help would be appreciated (I found a similar post here, but no
satisfactory anwser:
http://groups.google.com/group/nodejs/browse_thread/thread/14eb4cea5d8b85b1/2c87d208b572d0fa?lnk=gst),


Cheers, Stuart

Bradley Meck

unread,
Nov 10, 2011, 12:49:46 AM11/10/11
to nod...@googlegroups.com
Have you looked at the streams on children? Perhaps piping would do it for you : http://nodejs.org/docs/v0.6.0/api/all.html#child.stdin for example.

Axel Kittenberger

unread,
Nov 10, 2011, 3:50:04 AM11/10/11
to nod...@googlegroups.com
From the node prompt you can do this, works for me with 0.6.0

function shell() {  process.stdin.pause();
  require('tty').setRawMode(false);
  var ch = require('child_process').spawn('/bin/bash', [], {
customFds: [0, 1, 2]});  ch.on('exit',
function(){require('tty').setRawMode(true); process.stdin.resume()});
}
shell();

If you do not use readline in your app, you can remove the setRawMode() calls.

> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

Stuart Rackham

unread,
Nov 10, 2011, 9:34:44 PM11/10/11
to nodejs
Thanks very much for the help, I tried your shell function:

var shell, spawn, tty;
spawn = require('child_process').spawn;
tty = require('tty');

shell = function(cmd, opts) {
var p;
process.stdin.pause();
p = spawn(cmd, opts, {
customFds: [0, 1, 2]
});
return p.on('exit', function() {
process.stdin.resume();
return process.stdin.write("\x04");
});
};

shell('less', ['Cakefile']);

Works except you have to manually enter Ctrl+D to exit (I tried
writing Ctrl+D to stdin
at exit but it didn't have any effect).
If I setRawMode false then the Ctrl+D does not work
and you have to kill the node process from another console.

Cheers, Stuart
~
> >http://groups.google.com/group/nodejs/browse_thread/thread/14eb4cea5d...),

Stuart Rackham

unread,
Nov 10, 2011, 10:22:38 PM11/10/11
to nodejs
Async mindset blunder in my previous post.

All working now, here's the JS code:

8<--------------------
var shell, spawn, tty;

spawn = require('child_process').spawn;

tty = require('tty');

shell = function(cmd, opts, callback) {
var p;
process.stdin.pause();
tty.setRawMode(false);


p = spawn(cmd, opts, {
customFds: [0, 1, 2]
});
return p.on('exit', function() {

tty.setRawMode(true);
process.stdin.resume();
return callback();
});
};

shell('less', ['Cakefile'], function() {
return process.exit();
});
8<--------------------

Compiled from this CoffeeScript:

8<--------------------
{spawn} = require 'child_process'
tty = require 'tty'

shell = (cmd, opts, callback) ->
process.stdin.pause()
tty.setRawMode false
p = spawn cmd, opts, customFds: [0, 1, 2]
p.on 'exit', ->
tty.setRawMode true
process.stdin.resume()
callback()


shell 'less', ['Cakefile'], -> process.exit()
8<--------------------

Cheers, Stuart

侯志良

unread,
May 9, 2015, 6:50:27 PM5/9/15
to nod...@googlegroups.com
 require('child_process').spawn('less', ['Cakefile'], { stdio: 'inherit' });

在 2011年11月10日星期四 UTC+8上午11:39:43,Stuart Rackham写道:
Reply all
Reply to author
Forward
0 new messages