Ale*_*lls 3 stdout stderr child-process node.js
我正在使用Node.js(require('child_process'))创建一些child_processes,并且我想确保来自每个child_process的stdout/stderr不会转到终端,因为我只希望来自父进程的输出被记录.有没有办法将child_processes中的stdout/stderr流重定向到/dev/null其他不是终端的地方?
https://nodejs.org/api/child_process.html
也许只是:
var n = cp.fork('child.js',[],{
stdio: ['ignore','ignore','ignore']
});
Run Code Online (Sandbox Code Playgroud)
我只是尝试过,这似乎没有用.
现在我尝试了这个:
var stdout, stderr;
if (os.platform() === 'win32') {
stdout = fs.openSync('NUL', 'a');
stderr = fs.openSync('NUL', 'a');
}
else {
stdout = fs.openSync('/dev/null', 'a');
stderr = fs.openSync('/dev/null', 'a');
}
Run Code Online (Sandbox Code Playgroud)
然后这个选项:
stdio: ['ignore', stdout, stderr],
Run Code Online (Sandbox Code Playgroud)
但这没有做到,但似乎使用"detached:true"选项可能会使这项工作.
抛弃stdout和stderr分叉的子进程:
设置a pipe即silent = True在分叉时使用.
并将父流程上的stdout和stderr管道重定向到/dev/null.
为方便起见,options.stdio可能是以下字符串之一:
'pipe' - equivalent to ['pipe', 'pipe', 'pipe'] (the default)
'ignore' - equivalent to ['ignore', 'ignore', 'ignore']
'inherit' - equivalent to [process.stdin, process.stdout, process.stderr] or [0,1,2]
Run Code Online (Sandbox Code Playgroud)
显然childprocess.fork()不支持ignore; 只有childprocess.spawn().
fork确实支持silent允许在pipeOR 之间进行选择的选项inherit.
分叉子进程时:
If silent= True,然后是stdio = pipe.
如果silent= False,那么stdio = inherit.
silent
布尔如果为true,则将子节点的stdin,stdout和stderr传送给父节点,否则它们将从父节点继承.
有关更多详细信息,请参阅child_process.spawn()的stdio的'pipe'和'inherit'选项.
| 归档时间: |
|
| 查看次数: |
4834 次 |
| 最近记录: |