使用pm2启动一个节点应用程序,该节点应用程序派生一个子进程,当该子进程关闭时,pm2会重新启动该节点应用程序吗?

时间:2019-02-22 02:47:49

标签: javascript node.js child-process pm2

这是我遇到的问题。

我的节点项目中有三个文件,分别为app.jsapp-8080.jsapp-8090.js

除了端口外,这三个文件几乎相同。

app.js


server.listen(config.port0, () => {
    console.log(utilL.format('app is listening at http://%s:%s', config.host, config.port0));
});

app-8080.js


// the above is the same as app.js
server.listen(config.port1, () => {
    console.log(utilL.format('app is listening at http://%s:%s', config.host, config.port1));
});

app-8090.js


// the above is the same as app.js
server.listen(config.port2, () => {
    console.log(utilL.format('app is listening at http://%s:%s', config.host, config.port2));
});

更新app.js时,我必须做三次相同的工作。

现在我要进行如下更改。

app.js

let port = (/^\d+$/.test(process.argv[2])) ? process.argv[2] : config.port0;
server.listen(port, () => {
    console.log(utilL.format('app is listening at http://%s:%s', config.host, port));
});

app-8080.js

const childProcess = require('child_process');
const config = require('./config/config');
let port = config.port1;
childProcess.fork('./app.js', [port]);

可以吗?我使用PM2启动app-8080.js

命令如下:

$ pm2 start app-8080.js

app.js中发生错误时,pm2是否将重新启动该过程?

还有其他问题吗?还是更好的解决方案是什么?

很高兴收到您的答复!

0 个答案:

没有答案
相关问题