如何以编程方式关闭节点程序并重新启动它而不使用任何库?

问题描述 投票:2回答:1

我需要关闭一个节点程序并让它重新启动。我需要在自己的程序中执行此操作,而不必使用像永远需要设置的东西。

我知道我可以使用process.exit()来关闭程序,但是我能想到的任何东西都可以打开它,我可以从节点内启动它将被process.exit()杀死,然后才能完成。有没有办法在退出之前我没有看到从进程中分离exec调用?还有其他想法吗?我必须吸吮它并永远使用吗?

javascript node.js application-restart
1个回答
2
投票

快速而肮脏,测试不是很好:

var child_process = require('child_process');

process.on('SIGINT', function() {
  console.log('restarting...');
  child_process.fork(__filename); // TODO: pass args...
  process.exit(0);
});

console.log('Running as %d', process.pid);

setTimeout(function(){}, 1000000); // just some code to keep the process running
© www.soinside.com 2019 - 2024. All rights reserved.