作者:zhanhailiang 日期:2014-11-02
PM2是Node.js应用程序的进程管理管理,目前已在生产环境被普遍使用,提供以下特性:
- Transitional state of apps
- Process listing
- Automatic restart process based on memory
- Monitoring CPU/Memory usage
- Logs management
- Clustering
- Watch & Restart
- Reloading without downtime
- Make PM2 restart on server reboot
- JSON app declaration
详情请见:
接下来介绍PM2的基本使用方法:
1. 安装PM2:
[root@~/wade/git/node-lessons/lesson1]# npm install pm2 -g
2. PM2功能集:
[[email protected]~/wade/git/node-lessons/lesson1]# pm2 -h Usage: pm2 [cmd] app Commands: start [options] <file|json|stdin> start and daemonize an app deploy <file|environment> deploy your json startOrRestart <json> start or restart JSON file startOrReload <json> start or gracefully reload JSON file startOrGracefulReload <json> start or gracefully reload JSON file stop [options] <id|name|all|json|stdin> stop a process (to start it again, do pm2 restart <app>) restart [options] <id|name|all|json|stdin> restart a process reload <name|all> reload processes (note that its for app using HTTP/HTTPS) gracefulReload <name|all> gracefully reload a process. Send a "shutdown" message to close all connections. delete <name|id|script|all|json|stdin> stop and delete a process from pm2 process list sendSignal <signal> <pm2_id|name> send a system signal to the target process ping ping pm2 daemon - if not up it will launch it updatePM2 update in-memory PM2 with local PM2 update (alias) update in-memory PM2 with local PM2 interact [secret_key] [public_key] [machine_name] agent actions for keymetrics.io killInteract stop agent infoInteract get information about agent web launch an health API on port 9615 dump dump all processes for resurrecting them later save (alias) dump all processes for resurrecting them later resurrect resurrect previously dumped processes startup <platform> auto resurrect process at startup. [platform] = ubuntu, centos, gentoo or systemd generate generate an ecosystem.json configuration file ecosystem generate an ecosystem.json configuration file reset <name|id|all> reset counters for process describe <id> describe all parameters of a process id desc <id> describe all parameters of a process id list list all processes ls (alias) list all processes l (alias) list all processes status (alias) list all processes jlist list all processes in JSON format prettylist print json in a prettified JSON monit launch termcaps monitoring m (alias) launch termcaps monitoring flush flush logs reloadLogs reload all logs logs [id|name] stream logs file. Default stream all logs ilogs advanced interface to display logs kill kill daemon * Options: -h, --help output usage information -V, --version output the version number -v --version get version -s --silent hide all messages -m --mini-list display a compacted list without formatting -f --force force actions -n --name <name> set a <name> for script -i --instances <number> launch [number] instances (for networked app)(load balanced) -o --output <path> specify out log file -e --error <path> specify error log file -p --pid <pid> specify pid file --max-memory-restart <memory> specify max memory amount used to autorestart (in megaoctets) --env <environment_name> specify environment to get specific env variables (for JSON declaration) -x --execute-command execute a program using fork system -u --user <username> define user when generating startup script -c --cron <cron_pattern> restart a running process based on a cron pattern -w --write write configuration in local folder --interpreter <interpreter> the interpreter pm2 should use for executing app (bash, python...) --log-date-format <momentjs format> add custom prefix timestamp to logs --no-daemon run pm2 daemon in the foreground if it doesn‘t exist already --merge-logs merge logs from different instances but keep error and out separated --watch watch application folder for changes --node-args <node_args> space delimited arguments to pass to node in cluster mode - e.g. --node-args="--debug=7001 --trace-deprecation" --run-as-user <run_as_user> The user or uid to run a managed process as --run-as-group <run_as_group> The group or gid to run a managed process as --no-color skip colors Basic Examples: Start an app using all CPUs available + set a name : $ pm2 start app.js -i max --name "api" Restart the previous app launched, by name : $ pm2 restart api Stop the app : $ pm2 stop api Restart the app that is stopped : $ pm2 restart api Remove the app from the process list : $ pm2 delete api Kill daemon pm2 : $ pm2 kill Update pm2 : $ npm install [email protected] -g ; pm2 updatePM2 More examples in https://github.com/Unitech/pm2#usagefeatures Deployment help: $ pm2 deploy help
3. 使用PM2管理你的第一个Demo:
[root@~/wade/git/node-lessons/lesson1]# pm2 start app.js --name ‘firstapp‘ Starting PM2 daemon... [PM2] Process app.js launched ┌──────────┬────┬──────┬──────┬────────┬───────────┬────────┬─────────────┬─────────────┐ │ App name │ id │ mode │ PID │ status │ restarted │ uptime │ memory │ watching │ ├──────────┼────┼──────┼──────┼────────┼───────────┼────────┼─────────────┼─────────────┤ │ firstapp │ 0 │ fork │ 6616 │ online │ 0 │ 0s │ 11.145 MB │ unactivated │ └──────────┴────┴──────┴──────┴────────┴───────────┴────────┴─────────────┴─────────────┘ Use `pm2 desc[ribe] <id>` to get more details [root@~/wade/git/node-lessons/lesson1]# pm2 list ┌──────────┬────┬──────┬──────┬────────┬───────────┬────────┬─────────────┬─────────────┐ │ App name │ id │ mode │ PID │ status │ restarted │ uptime │ memory │ watching │ ├──────────┼────┼──────┼──────┼────────┼───────────┼────────┼─────────────┼─────────────┤ │ firstapp │ 0 │ fork │ 6616 │ online │ 0 │ 10s │ 18.539 MB │ unactivated │ └──────────┴────┴──────┴──────┴────────┴───────────┴────────┴─────────────┴─────────────┘ Use `pm2 desc[ribe] <id>` to get more details ^C[root@~/wade/git/node-lessons/lesson1]# pm2 desc firstapp Describing process with pid 0 - name firstapp ┌───────────────────┬────────────────────────────────────────────┐ │ status │ online │ │ name │ firstapp │ │ id │ 0 │ │ path │ /root/wade/git/node-lessons/lesson1/app.js │ │ args │ │ │ exec cwd │ /root/wade/git/node-lessons/lesson1 │ │ error log path │ /root/.pm2/logs/firstapp-err-0.log │ │ out log path │ /root/.pm2/logs/firstapp-out-0.log │ │ pid path │ /root/.pm2/pids/firstapp-0.pid │ │ mode │ fork_mode │ │ node v8 arguments │ │ │ watch & reload │ ? │ │ interpreter │ node │ │ restarts │ 0 │ │ unstable restarts │ 0 │ │ uptime │ 58s │ │ created at │ 2014-11-02T08:43:36.834Z │ └───────────────────┴────────────────────────────────────────────┘ [root@~/wade/git/node-lessons/lesson1]# pm2 stop firstapp [PM2] Stopping firstapp [PM2] stopProcessId process id 0 ┌──────────┬────┬──────┬──────┬─────────┬───────────┬────────┬────────┬─────────────┐ │ App name │ id │ mode │ PID │ status │ restarted │ uptime │ memory │ watching │ ├──────────┼────┼──────┼──────┼─────────┼───────────┼────────┼────────┼─────────────┤ │ firstapp │ 0 │ fork │ 6616 │ stopped │ 0 │ 0 │ 0 B │ unactivated │ └──────────┴────┴──────┴──────┴─────────┴───────────┴────────┴────────┴─────────────┘ Use `pm2 desc[ribe] <id>` to get more details [root@~/wade/git/node-lessons/lesson1]# pm2 kill [PM2] Stopping PM2... [PM2] Deleting all process [PM2] deleteProcessId process id 0 [PM2] All processes has been stopped and deleted [PM2] PM2 stopped
推荐阅读:
- PM2
/ ADVANCED_README.md - Unitech/PM2
- 告别node-forever,拥抱PM2
- 7
Tips for a Node.js Padawan - 使用Node.js完成的第一个项目的实践总结
时间: 2024-11-16 04:07:35