使用pm2启动nodeJS程序

[源引]https://github.com/Unitech/pm2
[官网]http://pm2.keymetrics.io/
[介绍]https://segmentfault.com/a/1190000002539204
pm2 是一个带有负载均衡功能的Node应用的进程管理器.
当你要把你的独立代码利用全部的服务器上的所有CPU,并保证进程永远都活着,0秒的重载, PM2是完美的。它非常适合IaaS结构,但不要把它用于PaaS方案(随后将开发Paas的解决方案).

安装

[root@9nve ~]# npm install -g pm2
/mnt/local/node/bin/pm2 -> /mnt/local/node/lib/node_modules/pm2/bin/pm2
/mnt/local/node/bin/pm2-dev -> /mnt/local/node/lib/node_modules/pm2/bin/pm2-dev
/mnt/local/node/bin/pm2-docker -> /mnt/local/node/lib/node_modules/pm2/bin/pm2-docker
/mnt/local/node/bin/pm2-runtime -> /mnt/local/node/lib/node_modules/pm2/bin/pm2-runtime
/mnt/local/node/lib
└─┬ pm2@2.7.1 
  ├─┬ async@2.5.0 
  │ └── lodash@4.17.4 
  ├── blessed@0.1.81 
  ├─┬ chalk@1.1.3 
  │ ├── ansi-styles@2.2.1 
  │ ├── escape-string-regexp@1.0.5 
  │ ├─┬ has-ansi@2.0.0 
  │ │ └── ansi-regex@2.1.1 
  │ ├── strip-ansi@3.0.1 
  │ └── supports-color@2.0.0 
  ├─┬ chokidar@1.7.0 
  │ ├─┬ anymatch@1.3.2 
  │ │ ├─┬ micromatch@2.3.11 
  │ │ │ ├─┬ arr-diff@2.0.0 
  │ │ │ │ └── arr-flatten@1.1.0 
  │ │ │ ├── array-unique@0.2.1 
  │ │ │ ├─┬ braces@1.8.5 
  │ │ │ │ ├─┬ expand-range@1.8.2 
  │ │ │ │ │ └─┬ fill-range@2.2.3 
  │ │ │ │ │   ├── is-number@2.1.0 
  │ │ │ │ │   ├── isobject@2.1.0 
  │ │ │ │ │   ├─┬ randomatic@1.1.7 
  │ │ │ │ │   │ ├─┬ is-number@3.0.0 
  │ │ │ │ │   │ │ └── kind-of@3.2.2 
  │ │ │ │ │   │ └── kind-of@4.0.0 
  │ │ │ │ │   └── repeat-string@1.6.1 
  │ │ │ │ ├── preserve@0.2.0 
  │ │ │ │ └── repeat-element@1.1.2 
  │ │ │ ├─┬ expand-brackets@0.1.5 
  │ │ │ │ └── is-posix-bracket@0.1.1 
  │ │ │ ├── extglob@0.3.2 
  │ │ │ ├── filename-regex@2.0.1 
  │ │ │ ├─┬ kind-of@3.2.2 
  │ │ │ │ └── is-buffer@1.1.5 
  │ │ │ ├─┬ object.omit@2.0.1 
  │ │ │ │ ├─┬ for-own@0.1.5 
  │ │ │ │ │ └── for-in@1.0.2 
  │ │ │ │ └── is-extendable@0.1.1 
  │ │ │ ├─┬ parse-glob@3.0.4 
  │ │ │ │ ├── glob-base@0.3.0 
  │ │ │ │ └── is-dotfile@1.0.3 
  │ │ │ └─┬ regex-cache@0.4.4 
  │ │ │   └─┬ is-equal-shallow@0.1.3 
  │ │ │     └── is-primitive@2.0.0 
  │ │ └─┬ normalize-path@2.1.1 
  │ │   └── remove-trailing-separator@1.1.0 
  │ ├── async-each@1.0.1 
  │ ├── glob-parent@2.0.0 
  │ ├── inherits@2.0.3 
  │ ├─┬ is-binary-path@1.0.1 
  │ │ └── binary-extensions@1.10.0 
  │ ├─┬ is-glob@2.0.1 
  │ │ └── is-extglob@1.0.0 
  │ ├── path-is-absolute@1.0.1 
  │ └─┬ readdirp@2.1.0 
  │   ├── graceful-fs@4.1.11 
  │   ├─┬ minimatch@3.0.4 
  │   │ └─┬ brace-expansion@1.1.8 
  │   │   ├── balanced-match@1.0.0 
  │   │   └── concat-map@0.0.1 
  │   ├─┬ readable-stream@2.3.3 
  │   │ ├── core-util-is@1.0.2 
  │   │ ├── isarray@1.0.0 
  │   │ ├── process-nextick-args@1.0.7 
  │   │ ├── safe-buffer@5.1.1 
  │   │ ├── string_decoder@1.0.3 
  │   │ └── util-deprecate@1.0.2 
  │   └── set-immediate-shim@1.0.1 
  ├── cli-table-redemption@1.0.1 
  ├── commander@2.11.0 
  ├─┬ cron@1.2.1 
  │ └── moment-timezone@0.5.13 
  ├─┬ debug@3.1.0 
  │ └── ms@2.0.0 
  ├── eventemitter2@1.0.5 
  ├── fclone@1.0.11 
  ├── gkt@1.0.0 
  ├─┬ mkdirp@0.5.1 
  │ └── minimist@0.0.8 
  ├── moment@2.19.1 
  ├─┬ needle@1.6.0 
  │ ├── debug@2.6.9 
  │ └── iconv-lite@0.4.19 
  ├─┬ nssocket@0.6.0 
  │ ├── eventemitter2@0.4.14 
  │ └── lazy@1.0.11 
  ├── pidusage@1.1.6 
  ├─┬ pm2-axon@3.1.0 
  │ ├── amp@0.3.1 
  │ ├── amp-message@0.1.2 
  │ └── escape-regexp@0.0.1 
  ├── pm2-axon-rpc@0.5.0 
  ├─┬ pm2-deploy@0.3.8 
  │ ├── async@1.5.2 
  │ └── tv4@1.3.0 
  ├─┬ pm2-multimeter@0.1.2 
  │ └── charm@0.1.2 
  ├─┬ pmx@1.5.4 
  │ ├── json-stringify-safe@5.0.1 
  │ └─┬ vxx@1.2.2 
  │   ├─┬ continuation-local-storage@3.2.0 
  │   │ ├── async-listener@0.6.8 
  │   │ └─┬ emitter-listener@1.0.1 
  │   │   └── shimmer@1.0.0 
  │   ├── debug@2.6.9 
  │   ├── extend@3.0.1 
  │   ├── is@3.2.1 
  │   ├── lodash.findindex@4.6.0 
  │   ├── lodash.isequal@4.5.0 
  │   ├── lodash.merge@4.6.0 
  │   ├── methods@1.1.2 
  │   ├── shimmer@1.1.0 
  │   └── uuid@3.1.0 
  ├─┬ promptly@2.2.0 
  │ └─┬ read@1.0.7 
  │   └── mute-stream@0.0.7 
  ├── semver@5.4.1 
  ├─┬ shelljs@0.7.8 
  │ ├─┬ glob@7.1.2 
  │ │ ├── fs.realpath@1.0.0 
  │ │ ├─┬ inflight@1.0.6 
  │ │ │ └── wrappy@1.0.2 
  │ │ └── once@1.4.0 
  │ ├── interpret@1.0.4 
  │ └─┬ rechoir@0.6.2 
  │   └─┬ resolve@1.4.0 
  │     └── path-parse@1.0.5 
  ├─┬ source-map-support@0.4.18 
  │ └── source-map@0.5.7 
  ├── sprintf-js@1.1.1 
  ├─┬ vizion@0.2.13 
  │ └── async@1.5.2 
  └─┬ yamljs@0.3.0 
    └─┬ argparse@1.0.9 
      └── sprintf-js@1.0.3 

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules/pm2/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"ia32"})

建立系统命令

ln -s /mnt/local/node/bin/pm2 /mnt/local/bin/pm2

启动程序

[root@9nve ~]# pm2 start /mnt/www/adminMongo/app.js

                        -------------

__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
 _\/\\\/\\\_\/\\\\\\________/\\\\\\__/\\\///\\\___
  _\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
   _\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
    _\/\\\/____\/\\\__\///\\\/___\/\\\________/\\\//_____
     _\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
      _\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
       _\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
        _\///______________\///______________\///__\///__


               Production Process Manager for Node.js apps
                     with a built-in Load Balancer.


                Start and Daemonize any application:
                $ pm2 start app.js

                Load Balance 4 instances of api.js:
                $ pm2 start api.js -i 4

                Monitor in production:
                $ pm2 monitor

                Make pm2 auto-boot at server restart:
                $ pm2 startup

                To go further checkout:
                http://pm2.io


                        -------------

[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /mnt/www/adminMongo/app.js in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬──────┬───────────┬──────┬──────────┐
│ App name │ id │ mode │ pid  │ status │ restart │ uptime │ cpu  │ mem       │ user │ watching │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼──────┼───────────┼──────┼──────────┤
│ app      │ 0  │ fork │ 4074 │ online │ 0       │ 0s     │ 135% │ 19.5 MB   │ root │ disabled │
└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴──────┴───────────┴──────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app

重启程序

[root@9nve ~]# pm2 restart /mnt/www/adminMongo/app.js
[PM2] Applying action stopProcessId on app [/mnt/www/adminMongo/app.js](ids: 0)
[PM2] [app](0) ✓
┌──────────┬────┬──────┬─────┬─────────┬─────────┬────────┬─────┬────────┬──────┬──────────┐
│ App name │ id │ mode │ pid │ status  │ restart │ uptime │ cpu │ mem    │ user │ watching │
├──────────┼────┼──────┼─────┼─────────┼─────────┼────────┼─────┼────────┼──────┼──────────┤
│ app      │ 0  │ fork │ 0   │ stopped │ 0       │ 0      │ 0%  │ 0 B    │ root │ disabled │
└──────────┴────┴──────┴─────┴─────────┴─────────┴────────┴─────┴────────┴──────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app

关闭程序

[root@9nve ~]# pm2 stop /mnt/www/adminMongo/app.js
[PM2] Applying action stopProcessId on app [/mnt/www/adminMongo/app.js](ids: 0)
[PM2] [app](0) ✓
┌──────────┬────┬──────┬─────┬─────────┬─────────┬────────┬─────┬────────┬──────┬──────────┐
│ App name │ id │ mode │ pid │ status  │ restart │ uptime │ cpu │ mem    │ user │ watching │
├──────────┼────┼──────┼─────┼─────────┼─────────┼────────┼─────┼────────┼──────┼──────────┤
│ app      │ 0  │ fork │ 0   │ stopped │ 0       │ 0      │ 0%  │ 0 B    │ root │ disabled │
└──────────┴────┴──────┴─────┴─────────┴─────────┴────────┴─────┴────────┴──────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值