[关闭]
@zhenxi 2017-02-15T13:00:12.000000Z 字数 6274 阅读 2927

pm2部署Asch程序

Asch PM2 NODEJS


1. 前言

1.1 部署前请先安装Asch

请参考官方说明:https://github.com/sqfasd/asch

1.2 pm2简介

pm2可以使node服务在后台运行(类似于linux的nohup),另外它可以在服务因异常或其他原因被杀掉后进行自动重启。由于Node的单线程特征,自动重启能很大程度上的提高它的健壮性。保证进程永远都活着,0秒的重载,PM2是完美的。

更多信息请查看:
pm2 官网:http://pm2.keymetrics.io/
pm2 github:https://github.com/Unitech/pm2

1.3 pm2安装和升级

  1. root@zhenxi-test: npm install -g pm2

1.4 pm2用法

  1. 基础用法:
  2. $ pm2 start app.js --name my-api # 命名进程为my-api
  3. $ pm2 list # 显示所有进程状态
  4. $ pm2 monit # 监视所有进程
  5. $ pm2 logs # 显示所有进程日志
  6. $ pm2 startup # 产生init 脚本保持进程活着
  7. $ pm2 web # 运行健壮的 computer API endpoint (http://localhost:9615)
  8. $ pm2 reload all/app_name # 0秒停机重载进程,会算在服务重启的次数中,类似于平滑重启
  9. $ pm2 restart id/all/app_name # 会重新加载代码,因为需要杀掉原有进程,所以服务会中断
  10. $ pm2 stop id/all/app_name # 停止指定名称的进程,如果是一个名称多进程,则一起停止,不会释放端口
  11. $ pm2 delete id/all/app_name # 删除指定名称的进程,如果是一个名称多进程,则一起删除,不会释放端口
  12. $ pm2 kill # 杀掉所有pm2进程并释放资源,包含pm2自身,会释放端口
  13. $ pm2 updatePM2 # 更新内存里的pm2

2. pm2部署Asch案例

本案例演示了如何利用pm2进行Asch程序的基本管理维护、升级等工作。

2.1 用pm2启动asch的app.js并将应用名字设置为“asch”

  1. root@zhenxi-test:/data/asch/1.1.5# pm2 start app.js --name asch
  2. [PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
  3. [PM2] PM2 Successfully daemonized
  4. [PM2] Starting /data/asch/1.1.5/app.js in fork_mode (1 instance)
  5. [PM2] Done.
  6. ┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐
  7. App name id mode pid status restart uptime cpu mem watching
  8. ├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤
  9. asch 0 fork 25724 online 0 0s 4% 14.1 MB disabled
  10. └──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────────┘
  11. Use `pm2 show <id|name>` to get more details about an app

2.2 将asch设置为开机启动

执行如下命令后asch后,pm2会开机自动自动,然后自动启动asch程序,解决机器意外重启后asch不启动、内存溢出导致程序退出等问题。

  1. root@zhenxi-test:/data/asch/1.1.5# pm2 startup
  2. [PM2] Writing startup script in /etc/init.d/pm2-init.sh
  3. [PM2] Making script booting at startup...
  4. [PM2] -linux- Using the command:
  5. su -c "chmod +x /etc/init.d/pm2-init.sh && update-rc.d pm2-init.sh defaults"
  6. Adding system startup for /etc/init.d/pm2-init.sh ...
  7. /etc/rc0.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
  8. /etc/rc1.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
  9. /etc/rc6.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
  10. /etc/rc2.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
  11. /etc/rc3.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
  12. /etc/rc4.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
  13. /etc/rc5.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
  14. [PM2] Done.
  15. [PM2] Now you can type
  16. $ pm2 save
  17. [PM2] To save the current process list at reboot or via pm2 update
  1. root@zhenxi-test:/data/asch/1.1.5# pm2 save
  2. [PM2] Saving current process list...
  3. [PM2] Successfully saved in /root/.pm2/dump.pm2

2.3 pm2重启asch

  1. root@zhenxi-test:/data/asch/1.1.5# pm2 restart asch
  2. Restarts are now immutable, to update environment or conf use --update-env
  3. [PM2] Applying action restartProcessId on app [asch](ids: 0)
  4. [PM2] [asch](0)
  5. ┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬──────────┬──────────┐
  6. App name id mode pid status restart uptime cpu mem watching
  7. ├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼──────────┼──────────┤
  8. asch 0 fork 25750 online 1 0s 0% 8.4 MB disabled
  9. └──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴──────────┴──────────┘
  10. Use `pm2 show <id|name>` to get more details about an app

2.4 pm2停止asch

  1. root@zhenxi-test:/data/asch/1.1.5# pm2 stop asch
  2. [PM2] Applying action stopProcessId on app [asch](ids: 0)
  3. [PM2] [asch](0)
  4. ┌──────────┬────┬──────┬─────┬─────────┬─────────┬────────┬─────┬────────┬──────────┐
  5. App name id mode pid status restart uptime cpu mem watching
  6. ├──────────┼────┼──────┼─────┼─────────┼─────────┼────────┼─────┼────────┼──────────┤
  7. asch 0 fork 0 stopped 1 0 0% 0 B disabled
  8. └──────────┴────┴──────┴─────┴─────────┴─────────┴────────┴─────┴────────┴──────────┘
  9. Use `pm2 show <id|name>` to get more details about an app

2.5 pm2下对asch进行升级

a. 首先用pm2停止asch应用:pm2 stop asch
b. 执行asch的升级命令或者升级脚本
c. 升级完成后停止asch程序:./aschd stop
d. 用pm2启动asch:pm2 start app.js --name asch

2.6 pm2查看已启动的应用列表

  1. root@zhenxi-test:/data/asch/1.1.5# pm2 list
  2. ┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐
  3. App name id mode pid status restart uptime cpu mem watching
  4. ├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤
  5. asch 0 fork 25791 online 1 6s 50% 86.1 MB disabled
  6. └──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────────┘
  7. Use `pm2 show <id|name>` to get more details about an app

2.7 检查日志

该日志包含pm2自身、应用程序的日志(支持实时刷新,类似linxu的tail -f模式)

  1. root@zhenxi-test:/data/asch/1.1.5# pm2 logs
  2. [TAILING] Tailing last 10 lines for [all] processes (change the value with --lines option)
  3. /root/.pm2/pm2.log last 10 lines:
  4. PM2 | 2016-11-10 17:05:49: pid=25724 msg=failed to kill - retrying in 100ms
  5. PM2 | 2016-11-10 17:05:49: App [asch] with id [0] and pid [25724], exited with code [1] via signal [SIGINT]
  6. PM2 | 2016-11-10 17:05:49: pid=25724 msg=process killed
  7. PM2 | 2016-11-10 17:05:49: Starting execution sequence in -fork mode- for app name:asch id:0
  8. PM2 | 2016-11-10 17:05:49: App name:asch id:0 online
  9. PM2 | 2016-11-10 17:06:31: Stopping app:asch id:0
  10. PM2 | 2016-11-10 17:06:31: App [asch] with id [0] and pid [25750], exited with code [1] via signal [SIGINT]
  11. PM2 | 2016-11-10 17:06:31: pid=25750 msg=process killed
  12. PM2 | 2016-11-10 17:11:42: Starting execution sequence in -fork mode- for app name:asch id:0
  13. PM2 | 2016-11-10 17:11:42: App name:asch id:0 online
  14. /root/.pm2/logs/asch-error-0.log last 10 lines:
  15. 0|asch | Error: write EPIPE
  16. /root/.pm2/logs/asch-out-0.log last 10 lines:
  17. 0|asch | log 2016-11-10 09:15:57 642 Block 10611506799826321747 loaded from 123.56.49.186:8192 at 41903
  18. 0|asch | log 2016-11-10 09:15:57 650 Block 3908455529014235549 loaded from 123.56.49.186:8192 at 41904
  19. 0|asch | log 2016-11-10 09:15:57 663 Block 16794902304099828850 loaded from 123.56.49.186:8192 at 41905
  20. 0|asch | log 2016-11-10 09:15:57 675 Block 10347224363093915554 loaded from 123.56.49.186:8192 at 41906

2.8 将应用从pm2中删除

将asch从pm2中删除后,则开机不会再自动启动asch程序。

  1. root@zhenxi-test:/data/asch/1.1.5# pm2 delete asch
  2. [PM2] Applying action deleteProcessId on app [asch](ids: 0)
  3. [PM2] [asch](0)
  4. ┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬─────┬──────────┐
  5. App name id mode pid status restart uptime cpu mem watching
  6. └──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴─────┴──────────┘
  7. Use `pm2 show <id|name>` to get more details about an app

2.9 退出pm2

停止pm2中的所有程序并删除后,pm2程序自身也会退出。
当你完全不想用pm2的时候可以执行这个命令

  1. root@zhenxi-test:/data/asch/1.1.5# pm2 kill
  2. [PM2] Stopping PM2...
  3. [PM2][WARN] No process found
  4. [PM2] All processes have been stopped and deleted
  5. [PM2] PM2 stopped
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注