Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS: child_process.exec not executing function

I'm attempting to do something I think is very simple -- execute an 'echo' line using child_process.exec. My code looks as such:

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

exec('echo "HELLO"', function (error, stdout, stderr) {
    console.log(error);
    console.log(stdout);
    console.log(stderr);
});

As of now, the echo is never being called and neither is the callback. Is there something I'm missing here?

Also, I'm using thins inside a grunt task I'm creating, so I'm not sure if there's anything there that could set it off (though this plugin seems to be doing it fine here --https://github.com/sindresorhus/grunt-shell/blob/master/tasks/shell.js)

like image 968
streetlight Avatar asked Jun 19 '14 18:06

streetlight


1 Answers

Your code is good.

The problem you have is probably about the callback, if you use execSync of child_process it will probably work.

If you want to keep exec function with callback in your grunt task, use this.async();

const done = this.async();
exec('echo "HELLO"', function (error, stdout, stderr) {
    console.log(error);
    console.log(stdout);
    console.log(stderr);
    done();
});

Have a look here: https://gruntjs.com/api/inside-tasks

like image 51
jpiazzal Avatar answered Oct 05 '22 09:10

jpiazzal