How to get the output of a spawned child_process in Node.JS?
Asked Answered
D

1

16

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

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

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

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)

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 :)

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.

Dov answered 29/4, 2016 at 9:34 Comment(0)
G
18

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

    var spawn = require('child_process').spawnSync;
    var child = spawn('ls' , ['-l','/usr']);
    console.log('stdout here: \n' + child.stdout);
Glandulous answered 29/4, 2016 at 11:43 Comment(4)
It gives me this error : child.stdout.on('data', function (data) { console.log('ls command output: ' + data); }); ^ TypeError: Cannot read property 'on' of nullDov
edited the answer with minor changes and it seems to be working for me. try again. I am using node version v4.2.4 BTW.Glandulous
It works well with spawn, thanks :) But I need it to work with spawnSync, as I want to wait for each command to end before going to another. As of now, I have a working function that execute the command, but NodeJS moves on in the program even before the command is able to return anything.Dov
It does work for me, thanks ! :) I've set up this code which works well : function command(txt) { var words = txt.trim().split(" "); var child = spawn(words[0] , words.splice(1, words.length)); return('' + child.stdout); } Do you know how I could make it so it works with pipes also ? Like in this line : grep sh | ls -l Edit : I'm sorry, the code looks awful when it's not indentedDov

© 2022 - 2024 — McMap. All rights reserved.