在 node.js 中使用参数生成过程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12778596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spawning process with arguments in node.js
提问by Industrial
I need to spawn a child process from node.js, whilst using ulimitto keep it from using to much memory.
我需要从 生成一个子进程node.js,同时使用ulimit以防止它使用太多内存。
Following the docs, it wasn't hard to get the basic spawn working: child = spawn("coffee", ["app.coffee"]).
按照文档,让基本的 spawn 工作并不难:child = spawn("coffee", ["app.coffee"]).
However, doing what I do below just makes the spawn die silently.
然而,做我在下面所做的只会让 spawn 默默地死去。
child = spawn("ulimit", ["-m 65536;", "coffee app.coffee"])
If I would run ulimit -m 65536; coffee app.coffee- it works as intented.
如果我要跑ulimit -m 65536; coffee app.coffee- 它会按预期工作。
What am I doing wrong here?
我在这里做错了什么?
回答by vinayr
Those are two different commands. Don't club them if you are using spawn. Use separate child processes.
这是两个不同的命令。如果您正在使用spawn. 使用单独的子进程。
child1 = spawn('ulimit', ['-m', '65536']);
child2 = spawn('coffee', ['app.coffee']);
If you are not interested in output stream(if you want just buffered output) you can use exec.
如果您对输出流不感兴趣(如果您只想缓冲输出),您可以使用exec.
var exec = require('child_process').exec,
child;
child = exec('ulimit -m 65536; coffee app.coffee',
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
}
});

