如何以编程方式关闭节点程序并重新启动它而不使用任何库? [英] How can I programmatically shutdown a node program and restart it without using any libraries?

查看:126
本文介绍了如何以编程方式关闭节点程序并重新启动它而不使用任何库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to shutdown a node program and have it restart. I need to do this within the itself program without having to use something like forever needing to be setup.

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

I know I can use process.exit() to shut the program down, but anything I can think of that would open it back up that I can kick off from within node would be killed by process.exit() before it could finish. Is there a way I'm not seeing to detach a exec call from the process before I exit? Any other ideas? Do I have to suck it up and go use forever?

推荐答案

快速而肮脏,未经过充分测试:

Quick and dirty, not very well tested:

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

这篇关于如何以编程方式关闭节点程序并重新启动它而不使用任何库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆