admin管理员组

文章数量:1404923

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 --.js)

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./sindresorhus/grunt-shell/blob/master/tasks/shell.js)

Share Improve this question edited Jun 19, 2014 at 18:08 streetlight asked Jun 19, 2014 at 18:01 streetlightstreetlight 5,97813 gold badges66 silver badges102 bronze badges 8
  • The argument is missing a quote to end the JavaScript string, 'echo "HELLO"'. Is that just in the question? Do you receive any errors? Since it's within a Grunt task, is the task defined to be asynchronous? – Jonathan Lonowski Commented Jun 19, 2014 at 18:03
  • Thanks for catching that -- just in the question. Im not getting any errors. As for the second part, I'm not sure -- I would just like run a mand from my task but seem to be getting a bit lost. – streetlight Commented Jun 19, 2014 at 18:09
  • @JonathanLonowski If it's helpful, here is the plugin I'm working on -- github./streetlight/grunt-github-changes – streetlight Commented Jun 19, 2014 at 18:15
  • If I run that in NodeJS v0.10.28 it works, displays null and then HELLO and a blank string. – tadman Commented Jun 19, 2014 at 18:25
  • 4 "Why doesn't my asynchronous task plete?" You'll have to use this.async() and the returned callback within the task or Grunt will interrupt the exec(). – Jonathan Lonowski Commented Jun 19, 2014 at 18:31
 |  Show 3 more ments

1 Answer 1

Reset to default 3

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./api/inside-tasks

本文标签: javascriptNodeJS childprocessexec not executing functionStack Overflow