时间:2021-05-14 10:40:20 | 栏目:NodeJS | 点击:次
接下来,就让我们看看forever能不能实现目标。
一、forever介绍
forever是一个简单的命令式nodejs的守护进程,能够启动,停止,重启App应用。forever完全基于命令行操作,在forever进程之下,创建node的子进程,通过monitor监控node子进程的运行情况,一旦文件更新,或者进程挂掉,forever会自动重启node服务器,确保应用正常运行。
二、 forever安装
全局安装forever
我们看到forever支持的命令和配置项确实不少,应该是偏命令行的管理工具。
三、forever命令行的中文解释
子命令actions:
四、forever服务器管理
创建一个web项目(express3+ejs),使用forever管理服务器。
安装express3
打开浏览器: http://localhost:3000,可以看到web界面
在win下面查看forever状态
~ D:\workspace\javascript\nodejs-forever>forever stop app.js
error: Forever cannot find process with index: app.js
五、forever在Ubuntu进行服务器管理
Linux的系统环境
Linux: Ubuntu 12.04.2 64bit Server
Node: v0.11.2
Npm: 1.2.21
初始化项目:安装命令不解释了
# 系统进程
~ ps -aux|grep node
Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html
conan 18296 0.5 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.4 0.8 630340 18392 ? Sl 15:48 0:00 /usr/local/bin/node /home/conan/nodejs/nodejs-forever/app.js
# 端口占用
~ netstat -nltp|grep node
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN 18299/node
停止服务器
我们看到在Linux Ubuntu环境中是正常的。
六、模拟服务器宕机
两种测试方案:
1. 用Linux命令,直接杀死node进程
2. 在应用中,模拟异常退出
1). 用Linux命令,直接杀死node进程
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
杀死forever的monitor
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
2). 在应用中,模拟异常退出
修改文件: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);
/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)
通过forever命令启动
# 检查错误日志
~ 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)
/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 3 time
Thu Sep 26 2013 16:15:33 GMT+0800 (CST)
Express server listening on port 3000
Thu Sep 26 2013 16:15:43 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 4 time
通过list我们手动刷新几次也可以看到pid是变的。
~ forever list
info: Forever processes running
data: uid command script forever pid logfile uptime
data: [0] SmtT /usr/local/bin/node app.js 18444 18579 logs/access.log 0:0:0:8.921
~ forever list
info: Forever processes running
data: uid command script forever pid logfile uptime
data: [0] SmtT /usr/local/bin/node app.js 18444 18604 logs/access.log 0:0:0:0.177
~ forever list
info: Forever processes running
data: uid command script forever pid logfile uptime
data: [0] SmtT /usr/local/bin/node app.js 18444 18604 logs/access.log 0:0:0:2.206
比起upstart管理,省略了配置脚本的步骤(/etc/init/nodejs-xx.conf)。其他的功能,还要更近一步的使用才知道。
七、开发环境和生产环境的启动配置
开发环境
~ forever -p $APP_PATH -l $LOG/access.log -e $LOG/error.log -o $LOG/out.log -a --pidFile $PID start $APP