跳到主要内容
CNode

nodejs如何以守护进程运行啊?

社区
Wwimlwiml发布于 15 年前最后回复 8 年前
11396370

一般是开一个ssh
然后:

node t.js  

如果这个ssh关闭,node也关闭了
如何让node t.js以守护进程运行啊???

查看回复

回复 (11)

J
JustinLiao1314#118 年前
const cp = require('child_process');
const child = cp.spawn('node', [__dirname + '/child.js'], { detached: true, stdio: 'ignore' });
process.exit(0);
G
goooto#1013 年前

可以使用forever吧。

M
mejinke#915 年前

我也是使用 nohup node 1.js > 1.log & 这种方式 参考:http://www.iblue.cc/2012/02/如何提高nodejs程序的稳定性/

W
wimlwiml#815 年前

谢谢WillWen和其它朋友,我暂时先用:
nohup node 1.js > 1.log &

W
willwen#715 年前

之前我在小田那里得到了一个更好的方法,这也是Joyent官方所推荐的:

var cluster = require('cluster');
if (cluster.isMaster) {
  //Fork a worker to run the main program
  for (var i = 0; i < 2; i++) var worker = cluster.fork();
} else {
  //Run main program
  require('./app.js');
  console.log('worker is running');
}

cluster.on('death', function(worker) {
  //If the worker died, fork a new worker
  console.log('worker ' + worker.pid + ' died. restart...');
  cluster.fork();
});

这个是我的MoeStream的保护进程,我加了一些注释,以便大家使用。

J
jsonshen#615 年前

nohup node app.js &

M
monwf#515 年前

linux下可以试试upstart

R
rench#415 年前

1.forever 2.node t.js &

S
shawny#315 年前

个人比较推崇使用screen

T
tank00#215 年前

后台进程。windows可以变成一个服务。

W
willwen#115 年前

nohup node 1 &

参与回复

登录后即可参与回复。登录