如何获取Node.JS中生成的child_process的输出?

时间:2022-01-23 00:01:19

First of all, I'm a complete noob and started using Node.JS yesterday (it was also my first time using Linux in years) so please be nice and explicit

首先,我是一个完整的菜鸟,并且昨天开始使用Node.JS(这也是我多年来第一次使用Linux)所以请保持良好和明确

I'm currently making a Node.JS program which has to, among other things, launch shell commands (mainly : mount an usb drive). I'm currently using

我目前正在制作Node.JS程序,其中包括启动shell命令(主要是:安装usb驱动器)。我正在使用

var spawn = require('child_process').spawnSync;

function shspawn(command) {
    spawn('sh', ['-c', command], { stdio: 'inherit' });
}

shspawn('echo Hello world');
shspawn('mkdir newdir');

etc. which is a really comfortable way to do it for me. The problem is that I'd like to store the output of, for example, a "ls" command in a variable, in a way like

这对我来说是一种非常舒适的方式。问题是我想以一种方式存储变量中的“ls”命令的输出,例如

var result = shspawn('ls -l')

I've read some examples online but they rarely use spawn and when they do, it doesn't work for me (I guess I may do something wrong, but again I'm a noob in Node)

我在线阅读了一些例子,但他们很少使用spawn,当他们这样做时,它对我不起作用(我想我可能做错了,但我又是Node中的菜鸟)

If you guys have a better idea than using child_process_spawnSync I'm open to any idea, but I'd like as long as possible to keep my program package-free :)

如果你们有一个比使用child_process_spawnSync更好的想法,我会接受任何想法,但我希望尽可能长时间保持我的程序包免费:)

EDIT : I need it to work synchronously ! That's why I've started using spawnSync. I will be using some commands like dd, that takes time and needs to be fully finished before the program moves on to another command.

编辑:我需要它同步工作!这就是我开始使用spawnSync的原因。我将使用像dd这样的命令,这需要花费时间并且需要在程序转移到另一个命令之前完全完成。

1 个解决方案

#1


6  

You can do it something like below.

你可以这样做,如下所示。

    var spawn = require('child_process').spawn;
    // Create a child process
    var child = spawn('ls' , ['-l']);

    child.stdout.on('data',
        function (data) {
            console.log('ls command output: ' + data);
        });
    child.stderr.on('data', function (data) {
        //throw errors
        console.log('stderr: ' + data);
    });

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

Update: with spawnSync

更新:使用spawnSync

    var spawn = require('child_process').spawnSync;
    var child = spawn('ls' , ['-l','/usr']);
    console.log('stdout here: \n' + child.stdout);

#1


6  

You can do it something like below.

你可以这样做,如下所示。

    var spawn = require('child_process').spawn;
    // Create a child process
    var child = spawn('ls' , ['-l']);

    child.stdout.on('data',
        function (data) {
            console.log('ls command output: ' + data);
        });
    child.stderr.on('data', function (data) {
        //throw errors
        console.log('stderr: ' + data);
    });

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

Update: with spawnSync

更新:使用spawnSync

    var spawn = require('child_process').spawnSync;
    var child = spawn('ls' , ['-l','/usr']);
    console.log('stdout here: \n' + child.stdout);