如何通过 NodeJS 子进程运行命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8389974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:51:20  来源:igfitidea点击:

How to run commands via NodeJS child process?

shellnode.jsprocesscommand-line-interfacecommand-prompt

提问by Tower

I am trying to run commands on Windows via NodeJS child processes:

我正在尝试通过 NodeJS 子进程在 Windows 上运行命令:

var terminal = require('child_process').spawn('cmd');

terminal.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
});

terminal.stderr.on('data', function (data) {
    console.log('stderr: ' + data);
});

terminal.on('exit', function (code) {
    console.log('child process exited with code ' + code);
});

setTimeout(function() {
    terminal.stdin.write('echo %PATH%');
}, 2000);

When it calls ti.stdin.write, it writes it to the stdindescriptor, but how do I trigger cmdto react at this point? How do I send the "enter" key signal that you do when you are actually typing in command prompt? Currently I get no response from cmd.

当它调用时ti.stdin.write,它会将它写入stdin描述符,但是此时我如何触发cmd以做出反应?当您实际输入命令提示符时,如何发送您执行的“输入”键信号?目前我没有收到来自cmd.

回答by toabi

Sending a newline \nwill exectue the command. .end()will exit the shell.

发送换行符\n将执行命令。.end()将退出外壳。

I modified the example to work with bash as I'm on osx.

我修改了示例以使用 bash,因为我在 osx 上。

var terminal = require('child_process').spawn('bash');

terminal.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
});

terminal.on('exit', function (code) {
    console.log('child process exited with code ' + code);
});

setTimeout(function() {
    console.log('Sending stdin to terminal');
    terminal.stdin.write('echo "Hello $USER. Your machine runs since:"\n');
    terminal.stdin.write('uptime\n');
    console.log('Ending terminal session');
    terminal.stdin.end();
}, 1000);

The output will be:

输出将是:

Sending stdin to terminal
Ending terminal session
stdout: Hello root. Your machine runs since:
stdout: 9:47  up 50 mins, 2 users, load averages: 1.75 1.58 1.42
child process exited with code 0

回答by Raivo Laanemets

You just have to send line end (\n) with the command:

您只需使用以下命令发送行尾 (\n):

setTimeout(function() {
    terminal.stdin.write('echo %PATH%\n');
}, 2000);

回答by cuixiping

You can use child_process exec method. here is an example:

您可以使用 child_process exec 方法。这是一个例子:

var exec = require('child_process').exec,
    child;

child = exec('echo %PATH%',
    function (error, stdout, stderr) {
        if(stdout!==''){
            console.log('---------stdout: ---------\n' + stdout);
        }
        if(stderr!==''){
            console.log('---------stderr: ---------\n' + stderr);
        }
        if (error !== null) {
            console.log('---------exec error: ---------\n[' + error+']');
        }
    });

回答by kgilpin

Make sure you stdin.end()at some point or the child process won't exit.

确保您stdin.end()在某个时候或子进程不会退出。