每次子进程以状态代码 8 退出时,我正在 fork 子进程,出于某种奇怪的原因。我做了一堆谷歌搜索试图找出什么是状态代码 8 以及什么可能导致 Ubuntu 中的这种奇怪行为但没有运气。我了解到 Node.js 甚至不使用退出状态代码 8,所以我非常有信心它不是 V8。我尝试在不同但相同的 Ubuntu 服务器上执行相同的代码,并且运行良好。请注意,我在此服务器上使用负载均衡器。我不确定是什么导致了这个问题。
规范如下:
- Node :v0.10.25
- 经销商 ID:Ubuntu
- 描述:Ubuntu 14.04
- 长期支持版本:14.04
这是 fork 的子进程(就像我说的最小)
process.on('message', function(data) { console.log('TEST BOT SAYS HELLO ' + process.pid) var fs = require('fs') fs.writeFile('message.txt', 'abc', function(err, data) { if (err) { return console.log(err); } console.log(data); }); });
编辑: 堆栈跟踪中没有其他错误。任何解决这个问题的线索都会有所帮助!
下面是子进程的启动方式:
function testChildProcess() { console.log('testing child process') var testBot = childProcess.fork(require.resolve("./../../bots/testBot")); testBot.send({ data: 'hello' }); testBot.on('exit', function(code, other) { console.log('Child process exited with exit code ' + code + ' other '); }); testBot.on('error', function(code) { console.log('Child process ERRED with exit code ' + code); return nextTracker(); }); } testChildProcess()
添加: 该应用程序使用 https://github.com/yyx990803/pod 在后台使用 pm2
https://github.com/Unitech/pm2 将应用程序作为守护进程启动。
我测试了生成子进程并且生成子进程工作得很好...
child .js
var test = function() { console.log('TEST BOT SAYS HELLO ' + process.pid) var fs = require('fs') fs.writeFile('./message.txt', 'abc', function(err, data) { if (err) { return console.log(err); } console.log(data); }); } test();
父类.js
function testChildProcess() { console.log(process.cwd()); var ls = childProcess.exec('node '+process.cwd()+'/bots/testBot', function(error, stdout, stderr) { if (error) { console.log(error.stack); console.log('Error code: ' + error.code); console.log('Signal received: ' + error.signal); } console.log('stdout: ' + stdout); console.log('stderr: ' + stderr); }); ls.on('exit', function(code) { console.log('Child process exited with exit code ' + code); }); }
解决方案: 以防其他人遇到此问题。我设法通过将 silent 设置为 true 来绕过。
var testBot = childProcess.fork(require.resolve("./../../bots/testBot"),[],{silent:true});
最佳答案
解决方案:为了防止其他人遇到此问题,我设法通过将 silent
设置为 true 来解决问题。
var testBot = childProcess.fork(require.resolve("./../../bots/testBot"),[],{silent:true});
关于node.js - Nodejs fork 子进程立即退出,状态码为 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27072478/