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 thenHELLO
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 theexec()
. – Jonathan Lonowski Commented Jun 19, 2014 at 18:31
1 Answer
Reset to default 3Your 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
版权声明:本文标题:javascript - NodeJS: child_process.exec not executing function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744865301a2629311.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论