conan 18296 0.0 1.1 597696 23776 ? Ssl 15:48 0:00 /usr/local/bin/node /usr/local/lib/node_modules/forever/bin/monitor app.js
conan 18299 0.0 0.8 630340 18392 ? Sl 15:48 0:00 /usr/local/bin/node /home/conan/nodejs/nodejs-forever/app.js
conan 18315 0.0 0.0 13584 956 pts/5 R+ 15:52 0:00 grep --color=auto node
# 杀死PID=19299
~ kill -9 18299
# 再看node进程,node自动重启,新的PID=18324
~ ps -aux|grep node
conan 18296 0.0 1.1 597696 23916 ? Ssl 15:48 0:00 /usr/local/bin/node /usr/local/lib/node_modules/forever/bin/monitor app.js
conan 18316 2.6 0.8 630340 18412 ? Sl 15:52 0:00 /usr/local/bin/node /home/conan/nodejs/nodejs-forever/app.js
conan 18324 0.0 0.0 13584 956 pts/5 R+ 15:52 0:00 grep --color=auto node
我们看到看杀死node进程,forever会帮助我们,重启node。
杀死forever的monitor
复制代码 代码如下:
~ kill -9 18296
~ ps -aux|grep node
conan 18316 0.0 0.9 630340 18644 ? Sl 15:52 0:00 /usr/local/bin/node /home/conan/nodejs/nodejs-forever/app.js
conan 18333 0.0 0.0 13584 952 pts/5 S+ 15:57 0:00 grep --color=auto node
# 再杀死node进程
~ kill -9 18316
~ ps -aux|grep node
conan 18336 0.0 0.0 13584 956 pts/5 S+ 15:58 0:00 grep --color=auto node
我们尝试杀死了forever的monitor,monitor程序没有自动重启,然后再杀死node进程后,node也不会自动重启了。
2). 在应用中,模拟异常退出
修改文件:app.js
复制代码 代码如下:
~ vi app.js
//..
http.createServer(app).listen(app.get('port'), function(){
console.log(new Date());
console.log('Express server listening on port ' + app.get('port'));
});
setTimeout(function(){
console.log(new Date());
throw new Error('App is error from inner!');
},10*1000);
通过node命令启动
复制代码 代码如下:
~ node app.js
Thu Sep 26 2013 16:08:44 GMT+0800 (CST)
Express server listening on port 3000
Thu Sep 26 2013 16:08:54 GMT+0800 (CST)
/home/conan/nodejs/nodejs-forever/app.js:41
throw new Error('App is error from inner!');
^
Error: App is error from inner!
at null._onTimeout (/home/conan/nodejs/nodejs-forever/app.js:41:9)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
10秒后,由于内部错误, node进程挂掉了。
通过forever命令启动
复制代码 代码如下:
~ mkdir logs
~ chmod 777 -R logs
~ forever -p . -l ./logs/access.log -e ./logs/error.log start app.js
# 检查错误日志
~ cat logs/access.log ls
Thu Sep 26 2013 16:15:02 GMT+0800 (CST)
Express server listening on port 3000
Thu Sep 26 2013 16:15:12 GMT+0800 (CST)
/home/conan/nodejs/nodejs-forever/app.js:41
throw new Error('App is error from inner!');
^
Error: App is error from inner!
at null._onTimeout (/home/conan/nodejs/nodejs-forever/app.js:41:9)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
error: Forever detected script exited with code: 8
error: Forever restarting script for 1 time
Thu Sep 26 2013 16:15:13 GMT+0800 (CST)
Express server listening on port 3000
Thu Sep 26 2013 16:15:23 GMT+0800 (CST)
/home/conan/nodejs/nodejs-forever/app.js:41
throw new Error('App is error from inner!');
^
Error: App is error from inner!
at null._onTimeout (/home/conan/nodejs/nodejs-forever/app.js:41:9)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
error: Forever detected script exited with code: 8
error: Forever restarting script for 2 time
Thu Sep 26 2013 16:15:23 GMT+0800 (CST)
Express server listening on port 3000
Thu Sep 26 2013 16:15:33 GMT+0800 (CST)