问题描述
我需要从 node.js
生成一个子进程,同时使用 ulimit
来防止它使用太多内存.
I need to spawn a child process from node.js
, whilst using ulimit
to keep it from using to much memory.
按照文档,让基本的 spawn 工作并不难:child = spawn("coffee", ["app.coffee"])
.
Following the docs, it wasn't hard to get the basic spawn working: child = spawn("coffee", ["app.coffee"])
.
然而,做我在下面所做的只会让 spawn 安静地死去.
However, doing what I do below just makes the spawn die silently.
child = spawn("ulimit", ["-m 65536;", "coffee app.coffee"])
如果我运行 ulimit -m 65536;咖啡 app.coffee
- 它按预期工作.
If I would run ulimit -m 65536; coffee app.coffee
- it works as intented.
我在这里做错了什么?
推荐答案
这是两个不同的命令.如果您正在使用 spawn
,请不要使用它们.使用单独的子进程.
Those are two different commands. Don't club them if you are using spawn
. Use separate child processes.
child1 = spawn('ulimit', ['-m', '65536']);
child2 = spawn('coffee', ['app.coffee']);
如果您对输出流不感兴趣(如果您只想缓冲输出),您可以使用 exec
.
If you are not interested in output stream(if you want just buffered output) you can use exec
.
var exec = require('child_process').exec,
child;
child = exec('ulimit -m 65536; coffee app.coffee',
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
}
});
这篇关于在 node.js 中使用参数生成过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!