@zhenxi
2017-02-15T13:00:12.000000Z
字数 6274
阅读 2927
Asch
PM2
NODEJS
请参考官方说明:https://github.com/sqfasd/asch
pm2可以使node服务在后台运行(类似于linux的nohup),另外它可以在服务因异常或其他原因被杀掉后进行自动重启。由于Node的单线程特征,自动重启能很大程度上的提高它的健壮性。保证进程永远都活着,0秒的重载,PM2是完美的。
更多信息请查看:
pm2 官网:http://pm2.keymetrics.io/
pm2 github:https://github.com/Unitech/pm2
root@zhenxi-test: npm install -g pm2
基础用法:
$ pm2 start app.js --name my-api # 命名进程为my-api
$ pm2 list # 显示所有进程状态
$ pm2 monit # 监视所有进程
$ pm2 logs # 显示所有进程日志
$ pm2 startup # 产生init 脚本保持进程活着
$ pm2 web # 运行健壮的 computer API endpoint (http://localhost:9615)
$ pm2 reload all/app_name # 0秒停机重载进程,会算在服务重启的次数中,类似于平滑重启
$ pm2 restart id/all/app_name # 会重新加载代码,因为需要杀掉原有进程,所以服务会中断
$ pm2 stop id/all/app_name # 停止指定名称的进程,如果是一个名称多进程,则一起停止,不会释放端口
$ pm2 delete id/all/app_name # 删除指定名称的进程,如果是一个名称多进程,则一起删除,不会释放端口
$ pm2 kill # 杀掉所有pm2进程并释放资源,包含pm2自身,会释放端口
$ pm2 updatePM2 # 更新内存里的pm2
本案例演示了如何利用pm2进行Asch程序的基本管理维护、升级等工作。
root@zhenxi-test:/data/asch/1.1.5# pm2 start app.js --name asch
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /data/asch/1.1.5/app.js in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤
│ asch │ 0 │ fork │ 25724 │ online │ 0 │ 0s │ 4% │ 14.1 MB │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
执行如下命令后asch后,pm2会开机自动自动,然后自动启动asch程序,解决机器意外重启后asch不启动、内存溢出导致程序退出等问题。
root@zhenxi-test:/data/asch/1.1.5# pm2 startup
[PM2] Writing startup script in /etc/init.d/pm2-init.sh
[PM2] Making script booting at startup...
[PM2] -linux- Using the command:
su -c "chmod +x /etc/init.d/pm2-init.sh && update-rc.d pm2-init.sh defaults"
Adding system startup for /etc/init.d/pm2-init.sh ...
/etc/rc0.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc1.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc6.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc2.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc3.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc4.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc5.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
[PM2] Done.
[PM2] Now you can type
$ pm2 save
[PM2] To save the current process list at reboot or via pm2 update
root@zhenxi-test:/data/asch/1.1.5# pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /root/.pm2/dump.pm2
root@zhenxi-test:/data/asch/1.1.5# pm2 restart asch
Restarts are now immutable, to update environment or conf use --update-env
[PM2] Applying action restartProcessId on app [asch](ids: 0)
[PM2] [asch](0) ✓
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬──────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼──────────┼──────────┤
│ asch │ 0 │ fork │ 25750 │ online │ 1 │ 0s │ 0% │ 8.4 MB │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴──────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
root@zhenxi-test:/data/asch/1.1.5# pm2 stop asch
[PM2] Applying action stopProcessId on app [asch](ids: 0)
[PM2] [asch](0) ✓
┌──────────┬────┬──────┬─────┬─────────┬─────────┬────────┬─────┬────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
├──────────┼────┼──────┼─────┼─────────┼─────────┼────────┼─────┼────────┼──────────┤
│ asch │ 0 │ fork │ 0 │ stopped │ 1 │ 0 │ 0% │ 0 B │ disabled │
└──────────┴────┴──────┴─────┴─────────┴─────────┴────────┴─────┴────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
a. 首先用pm2停止asch应用:pm2 stop asch
b. 执行asch的升级命令或者升级脚本
c. 升级完成后停止asch程序:./aschd stop
d. 用pm2启动asch:pm2 start app.js --name asch
root@zhenxi-test:/data/asch/1.1.5# pm2 list
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤
│ asch │ 0 │ fork │ 25791 │ online │ 1 │ 6s │ 50% │ 86.1 MB │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
该日志包含pm2自身、应用程序的日志(支持实时刷新,类似linxu的tail -f模式)
root@zhenxi-test:/data/asch/1.1.5# pm2 logs
[TAILING] Tailing last 10 lines for [all] processes (change the value with --lines option)
/root/.pm2/pm2.log last 10 lines:
PM2 | 2016-11-10 17:05:49: pid=25724 msg=failed to kill - retrying in 100ms
PM2 | 2016-11-10 17:05:49: App [asch] with id [0] and pid [25724], exited with code [1] via signal [SIGINT]
PM2 | 2016-11-10 17:05:49: pid=25724 msg=process killed
PM2 | 2016-11-10 17:05:49: Starting execution sequence in -fork mode- for app name:asch id:0
PM2 | 2016-11-10 17:05:49: App name:asch id:0 online
PM2 | 2016-11-10 17:06:31: Stopping app:asch id:0
PM2 | 2016-11-10 17:06:31: App [asch] with id [0] and pid [25750], exited with code [1] via signal [SIGINT]
PM2 | 2016-11-10 17:06:31: pid=25750 msg=process killed
PM2 | 2016-11-10 17:11:42: Starting execution sequence in -fork mode- for app name:asch id:0
PM2 | 2016-11-10 17:11:42: App name:asch id:0 online
/root/.pm2/logs/asch-error-0.log last 10 lines:
0|asch | Error: write EPIPE
/root/.pm2/logs/asch-out-0.log last 10 lines:
0|asch | log 2016-11-10 09:15:57 642 Block 10611506799826321747 loaded from 123.56.49.186:8192 at 41903
0|asch | log 2016-11-10 09:15:57 650 Block 3908455529014235549 loaded from 123.56.49.186:8192 at 41904
0|asch | log 2016-11-10 09:15:57 663 Block 16794902304099828850 loaded from 123.56.49.186:8192 at 41905
0|asch | log 2016-11-10 09:15:57 675 Block 10347224363093915554 loaded from 123.56.49.186:8192 at 41906
将asch从pm2中删除后,则开机不会再自动启动asch程序。
root@zhenxi-test:/data/asch/1.1.5# pm2 delete asch
[PM2] Applying action deleteProcessId on app [asch](ids: 0)
[PM2] [asch](0) ✓
┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬─────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
└──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴─────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
停止pm2中的所有程序并删除后,pm2程序自身也会退出。
当你完全不想用pm2的时候可以执行这个命令
root@zhenxi-test:/data/asch/1.1.5# pm2 kill
[PM2] Stopping PM2...
[PM2][WARN] No process found
[PM2] All processes have been stopped and deleted
[PM2] PM2 stopped