Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

23.7. pm2

Production process manager for Node.js apps with a built-in load balancer http://pm2.io

		npm install -g pm2
	

23.7.1. 启动进程

			
[ethereum@netkiller web3.mainnet]$ pm2 start restful.v2.js 
[PM2] Applying action restartProcessId on app [restful.v2](ids: 0)
[PM2] [restful.v2](0) ✓
[PM2] Process successfully started
┌────────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────────┬──────────┐
│ App name   │ id │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user     │ watching │
├────────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────────┼──────────┤
│ restful.v2 │ 0  │ fork │ 11480 │ online │ 17      │ 0s     │ 0%  │ 10.3 MB   │ ethereum │ disabled │
└────────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
			
		

执行启动程序名称

			
[ethereum@netkiller web3.mainnet]$ pm2 start restful.v2.js --name restful
[PM2] Applying action restartProcessId on app [restful.v2](ids: 0)
[PM2] [restful.v2](0) ✓
[PM2] Process successfully started
┌────────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────────┬──────────┐
│ App name   │ id │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user     │ watching │
├────────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────────┼──────────┤
│ restful    │ 0  │ fork │ 11480 │ online │ 17      │ 0s     │ 0%  │ 10.3 MB   │ ethereum │ disabled │
└────────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
			
		

23.7.2. 停止程序运行

			
[ethereum@netkiller web3.mainnet]$ pm2 stop pm2-http-interface
[PM2] Applying action stopProcessId on app [pm2-http-interface](ids: 1)
[PM2] [pm2-http-interface](1) ✓
┌────────────────────┬────┬──────┬───────┬─────────┬─────────┬────────┬─────┬───────────┬──────────┬──────────┐
│ App name           │ id │ mode │ pid   │ status  │ restart │ uptime │ cpu │ mem       │ user     │ watching │
├────────────────────┼────┼──────┼───────┼─────────┼─────────┼────────┼─────┼───────────┼──────────┼──────────┤
│ pm2-http-interface │ 1  │ fork │ 0     │ stopped │ 0       │ 0      │ 0%  │ 0 B       │ ethereum │ disabled │
│ restful.v2         │ 0  │ fork │ 11480 │ online  │ 17      │ 11m    │ 0%  │ 26.6 MB   │ ethereum │ disabled │
└────────────────────┴────┴──────┴───────┴─────────┴─────────┴────────┴─────┴───────────┴──────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
			
		

23.7.3. 查看进程列表

			
[ethereum@netkiller web3.mainnet]$ pm2 list
┌────────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────────┬──────────┐
│ App name   │ id │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user     │ watching │
├────────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────────┼──────────┤
│ restful.v2 │ 0  │ fork │ 11480 │ online │ 17      │ 2m     │ 0%  │ 28.1 MB   │ ethereum │ disabled │
└────────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
			
		

23.7.4. 查看启动进程的详细信息

			
[ethereum@netkiller web3.mainnet]$ pm2 show restful.v2
 Describing process with id 0 - name restful.v2 
┌───────────────────┬─────────────────────────────────────────────────┐
│ status            │ online                                          │
│ name              │ restful.v2                                      │
│ restarts          │ 17                                              │
│ uptime            │ 4m                                              │
│ script path       │ /home/ethereum/web3.mainnet/restful.v2.js       │
│ script args       │ N/A                                             │
│ error log path    │ /home/ethereum/.pm2/logs/restful.v2-error-0.log │
│ out log path      │ /home/ethereum/.pm2/logs/restful.v2-out-0.log   │
│ pid path          │ /home/ethereum/.pm2/pids/restful.v2-0.pid       │
│ interpreter       │ node                                            │
│ interpreter args  │ N/A                                             │
│ script id         │ 0                                               │
│ exec cwd          │ /home/ethereum/web3.mainnet                     │
│ exec mode         │ fork_mode                                       │
│ node.js version   │ 9.11.1                                          │
│ watch & reload    │ ✘                                               │
│ unstable restarts │ 0                                               │
│ created at        │ 2018-04-24T01:37:40.184Z                        │
└───────────────────┴─────────────────────────────────────────────────┘
 Revision control metadata 
┌──────────────────┬────────────────────────────────────────────────────────────────┐
│ revision control │ git                                                            │
│ remote url       │ https://github.com/ibook/web3.example.git                      │
│ repository root  │ /home/ethereum/web3.mainnet                                    │
│ last update      │ 2018-04-24T01:41:31.017Z                                       │
│ revision         │ f8aaa8769a5e039e0aba44aee5dd2571e5b71032                       │
│ comment          │ Merge branch 'master' of https://github.com/ibook/web3.example │
│ branch           │ master                                                         │
└──────────────────┴────────────────────────────────────────────────────────────────┘
 Code metrics value 
┌─────────────────┬────────┐
│ Loop delay      │ 1.28ms │
│ Active requests │ 0      │
│ Active handles  │ 4      │
└─────────────────┴────────┘
 Add your own code metrics: http://bit.ly/code-metrics
 Use `pm2 logs restful.v2 [--lines 1000]` to display logs
 Use `pm2 monit` to monitor CPU and Memory usage restful.v2

			
		

23.7.5. 系统资源监控

			
[ethereum@netkiller web3.mainnet]$ pm2 monit

┌─ Process list ────────────┐┌─ Global Logs ───────────────────────────────────────────────────┐
│[ 0] restful.v2     Mem:   ││                                                                 │
│                           ││                                                                 │
│                           ││                                                                 │
│                           ││                                                                 │
│                           ││                                                                 │
│                           ││                                                                 │
│                           ││                                                                 │
│                           ││                                                                 │
│                           ││                                                                 │
│                           ││                                                                 │
│                           ││                                                                 │
│                           ││                                                                 │
│                           ││                                                                 │
│                           ││                                                                 │
└───────────────────────────┘└─────────────────────────────────────────────────────────────────┘
┌─ Custom metrics (http://b─┐┌─ Metadata ──────────────────────────────────────────────────────┐
│ Loop delay        1.05ms  ││ App Name              restful.v2                                │
│ Active requests        0  ││ Restarts              17                                        │
│ Active handles         4  ││ Uptime                6m                                        │
└───────────────────────────┘└─────────────────────────────────────────────────────────────────┘
 left/right: switch boards | up/down/mouse: scroll | Ctrl-C: exit        To go further check out 
			
		

23.7.6. 监控接口

pm2可以提供一个接口获取监控信息

			
[ethereum@netkiller web3.mainnet]$ pm2 web
Launching web interface on 0.0.0.0:9615
[PM2][WARN] Applications pm2-http-interface not running, starting...
[PM2] App [pm2-http-interface] launched (1 instances)
[PM2] Process launched
┌────────────────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────────┬──────────┐
│ App name           │ id │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user     │ watching │
├────────────────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────────┼──────────┤
│ pm2-http-interface │ 1  │ fork │ 11809 │ online │ 0       │ 0s     │ 0%  │ 2.7 MB    │ ethereum │ disabled │
│ restful.v2         │ 0  │ fork │ 11480 │ online │ 17      │ 8m     │ 0%  │ 26.6 MB   │ ethereum │ disabled │
└────────────────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
			
		

通过 http://localhost:9615 取得监控数据

			
[ethereum@netkiller web3.mainnet]$ curl http://localhost:9615
{"system_info":{"hostname":"netkiller","uptime":842027},"monit":{"loadavg":[2.017578125,2.232421875,2.29150390625],"total_mem":8202674176,"free_mem":143159296,"cpu":[{"model":"Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz","speed":2494,"times":{"user":4894565000,"nice":7400,"sys":438689200,"idle":1763658900,"irq":0}},{"model":"Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz","speed":2494,"times":{"user":4212099800,"nice":10400,"sys":300205500,"idle":3197573800,"irq":0}},{"model":"Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz","speed":2494,"times":{"user":4412640700,"nice":7000,"sys":334607800,"idle":2757973000,"irq":0}},{"model":"Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz","speed":2494,"times":{"user":4134519800,"nice":8700,"sys":297732100,"idle":3231274500,"irq":0}}],"interfaces":{"lo":[{"address":"127.0.0.1","netmask":"255.0.0.0","family":"IPv4","mac":"00:00:00:00:00:00","internal":true,"cidr":"127.0.0.1/8"}],"eth0":[{"address":"172.31.180.30","netmask":"255.255.240.0","family":"IPv4","mac":"00:16:3e:02:8b:75","internal":false,"cidr":"172.31.180.30/20"}]}},"processes":[{"pid":11480,"name":"restful.v2","pm2_env":{"username":"ethereum","windowsHide":true,"instance_var":"NODE_APP_INSTANCE","exec_mode":"fork_mode","watch":false,"treekill":true,"autorestart":true,"automation":true,"pmx":true,"vizion":true,"name":"restful.v2","node_args":[],"pm_exec_path":"/home/ethereum/web3.mainnet/restful.v2.js","env":{"PM2_INTERACTOR_PROCESSING":"true","PM2_USAGE":"CLI","_":"/srv/node/bin/pm2","HISTTIMEFORMAT":"%Y-%m-%d %H:%M:%S ","LESSOPEN":"||/usr/bin/lesspipe.sh %s","LOGNAME":"ethereum","GOROOT":"/srv/go","HOME":"/home/ethereum","SHLVL":"1","HISTCONTROL":"ignoredups","LANG":"zh_CN.UTF-8","PWD":"/home/ethereum/web3.mainnet","PATH":"/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/srv/go-ethereum/bin:/srv/go/bin:/srv/go-ipfs:/srv/node/bin::/home/ethereum/.local/bin:/home/ethereum/bin","MAIL":"/var/spool/mail/ethereum","LS_COLORS":"rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:","USER":"ethereum","TIME_STYLE":"long-iso","HISTSIZE":"1000","TERM":"xterm","SHELL":"/bin/bash","HOSTNAME":"netkiller","XDG_SESSION_ID":"392","PM2_HOME":"/home/ethereum/.pm2","restful.v2":{}},"pm_cwd":"/home/ethereum/web3.mainnet","exec_interpreter":"node","instances":1,"pm_out_log_path":"/home/ethereum/.pm2/logs/restful.v2-out-0.log","pm_err_log_path":"/home/ethereum/.pm2/logs/restful.v2-error-0.log","pm_pid_path":"/home/ethereum/.pm2/pids/restful.v2-0.pid","km_link":false,"vizion_running":false,"NODE_APP_INSTANCE":0,"PM2_INTERACTOR_PROCESSING":"true","PM2_USAGE":"CLI","_":"/srv/node/bin/pm2","HISTTIMEFORMAT":"%Y-%m-%d %H:%M:%S ","LESSOPEN":"||/usr/bin/lesspipe.sh %s","LOGNAME":"ethereum","GOROOT":"/srv/go","HOME":"/home/ethereum","SHLVL":"1","HISTCONTROL":"ignoredups","LANG":"zh_CN.UTF-8","PWD":"/home/ethereum/web3.mainnet","PATH":"/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/srv/go-ethereum/bin:/srv/go/bin:/srv/go-ipfs:/srv/node/bin::/home/ethereum/.local/bin:/home/ethereum/bin","MAIL":"/var/spool/mail/ethereum","LS_COLORS":"rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:","USER":"ethereum","TIME_STYLE":"long-iso","HISTSIZE":"1000","TERM":"xterm","SHELL":"/bin/bash","HOSTNAME":"netkiller","XDG_SESSION_ID":"392","PM2_HOME":"/home/ethereum/.pm2","status":"online","pm_uptime":1524533860184,"axm_actions":[],"axm_monitor":{"Loop delay":{"alert":{},"unit":"ms","type":"libuv/latency","historic":true,"agg_type":"avg","value":"0.91ms"},"Active requests":{"alert":{},"type":"Active requests","historic":true,"agg_type":"avg","value":0},"Active handles":{"alert":{},"type":"Active handles","historic":true,"agg_type":"avg","value":4}},"axm_options":{"default_actions":true,"transactions":false,"http":false,"http_latency":200,"http_code":500,"ignore_routes":[],"profiling":true,"errors":true,"alert_enabled":true,"custom_probes":true,"network":false,"ports":false,"ignoreFilter":{"method":["OPTIONS"],"url":[]},"excludedHooks":[],"deep_metrics":false,"event_loop_dump":false,"v8":false,"module_conf":{},"module_name":"restful.v2","module_version":"2.10.2","pmx_version":"1.6.4","error":true},"axm_dynamic":{},"created_at":1524533860184,"pm_id":0,"restart_time":17,"unstable_restarts":0,"node_version":"9.11.1","versioning":{"type":"git","url":"https://github.com/ibook/web3.example.git","revision":"f8aaa8769a5e039e0aba44aee5dd2571e5b71032","update_time":"2018-04-24T01:46:31.016Z","comment":"Merge branch 'master' of https://github.com/ibook/web3.example","unstaged":true,"branch":"master","remotes":["origin"],"remote":"origin","branch_exists_on_remote":true,"ahead":false,"next_rev":null,"prev_rev":"a42ab811b17763684ad25b24adcf10836f39f2c3","repo_path":"/home/ethereum/web3.mainnet"},"exit_code":1},"pm_id":0,"monit":{"memory":27947008,"cpu":0}},{"pid":11809,"name":"pm2-http-interface","pm2_env":{"username":"ethereum","windowsHide":true,"instance_var":"NODE_APP_INSTANCE","exec_mode":"fork_mode","env":{"pm2-http-interface":"{}","PM2_HOME":"/home/ethereum/.pm2","XDG_SESSION_ID":"394","HOSTNAME":"netkiller","SHELL":"/bin/bash","TERM":"xterm-256color","HISTSIZE":"1000","TIME_STYLE":"long-iso","USER":"ethereum","LS_COLORS":"rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:","MAIL":"/var/spool/mail/ethereum","PATH":"/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/srv/go-ethereum/bin:/srv/go/bin:/srv/go-ipfs:/srv/node/bin::/home/ethereum/.local/bin:/home/ethereum/bin","PWD":"/home/ethereum/web3.mainnet","LANG":"zh_CN.UTF-8","HISTCONTROL":"ignoredups","SHLVL":"1","HOME":"/home/ethereum","GOROOT":"/srv/go","LOGNAME":"ethereum","LESSOPEN":"||/usr/bin/lesspipe.sh %s","HISTTIMEFORMAT":"%Y-%m-%d %H:%M:%S ","_":"/srv/node/bin/pm2","PM2_USAGE":"CLI","PM2_JSON_PROCESSING":"true"},"treekill":true,"autorestart":true,"automation":true,"pmx":true,"vizion":true,"name":"pm2-http-interface","node_args":[],"pm_exec_path":"/srv/node-v9.11.1/lib/node_modules/pm2/lib/HttpInterface.js","pm_cwd":"/home/ethereum/web3.mainnet","exec_interpreter":"node","instances":1,"pm_out_log_path":"/home/ethereum/.pm2/logs/pm2-http-interface-out-1.log","pm_err_log_path":"/home/ethereum/.pm2/logs/pm2-http-interface-error-1.log","pm_pid_path":"/home/ethereum/.pm2/pids/pm2-http-interface-1.pid","km_link":false,"vizion_running":false,"NODE_APP_INSTANCE":0,"pm2-http-interface":"{}","PM2_HOME":"/home/ethereum/.pm2","XDG_SESSION_ID":"394","HOSTNAME":"netkiller","SHELL":"/bin/bash","TERM":"xterm-256color","HISTSIZE":"1000","TIME_STYLE":"long-iso","USER":"ethereum","LS_COLORS":"rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:","MAIL":"/var/spool/mail/ethereum","PATH":"/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/srv/go-ethereum/bin:/srv/go/bin:/srv/go-ipfs:/srv/node/bin::/home/ethereum/.local/bin:/home/ethereum/bin","PWD":"/home/ethereum/web3.mainnet","LANG":"zh_CN.UTF-8","HISTCONTROL":"ignoredups","SHLVL":"1","HOME":"/home/ethereum","GOROOT":"/srv/go","LOGNAME":"ethereum","LESSOPEN":"||/usr/bin/lesspipe.sh %s","HISTTIMEFORMAT":"%Y-%m-%d %H:%M:%S ","_":"/srv/node/bin/pm2","PM2_USAGE":"CLI","PM2_JSON_PROCESSING":"true","status":"online","pm_uptime":1524534352075,"axm_actions":[],"axm_monitor":{"Loop delay":{"alert":{},"unit":"ms","type":"libuv/latency","historic":true,"agg_type":"avg","value":"0.78ms"},"Active requests":{"alert":{},"type":"Active requests","historic":true,"agg_type":"avg","value":0},"Active handles":{"alert":{},"type":"Active handles","historic":true,"agg_type":"avg","value":4}},"axm_options":{"default_actions":true,"transactions":false,"http":false,"http_latency":200,"http_code":500,"ignore_routes":[],"profiling":true,"errors":true,"alert_enabled":true,"custom_probes":true,"network":false,"ports":false,"ignoreFilter":{"method":["OPTIONS"],"url":[]},"excludedHooks":[],"deep_metrics":false,"event_loop_dump":false,"v8":false,"module_conf":{},"module_name":"pm2-http-interface","module_version":"2.10.2","pmx_version":"1.6.4","error":true},"axm_dynamic":{},"created_at":1524534352075,"pm_id":1,"restart_time":0,"unstable_restarts":0,"versioning":null,"node_version":"9.11.1"},"pm_id":1,"monit":{"memory":24776704,"cpu":0}}]}
			
		

23.7.7. logs

			
$ pm2 logs project --lines 100
			
		

23.7.8. 设置开机启动

		
[root@localhost ~]# pm2 startup
[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
Restart=on-failure

ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/local/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-root.service
Command list
[ 'systemctl enable pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-root...
Created symlink /etc/systemd/system/multi-user.target.wants/pm2-root.service → /etc/systemd/system/pm2-root.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemd		
		
		

查看状态

		
[root@localhost ~]# systemctl status pm2-root
● pm2-root.service - PM2 process manager
   Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: https://pm2.keymetrics.io/		
		
		

有时我们不希望使用 root 启动程序,可以这样操作

		
[root@localhost ~]# su - www
Last login: Thu Jul 29 14:29:02 CST 2021 on pts/4
[www@localhost ~]$ pm2 startup
[PM2] Init System found: systemd
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u www --hp /home/www
[www@localhost ~]$ sudo env PATH=$PATH:/usr/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u www --hp /home/www
[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=www
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/home/www/.local/bin:/home/www/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/home/www/.pm2
PIDFile=/home/www/.pm2/pm2.pid
Restart=on-failure

ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/local/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-www.service
Command list
[ 'systemctl enable pm2-www' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-www.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-www...
Created symlink /etc/systemd/system/multi-user.target.wants/pm2-www.service → /etc/systemd/system/pm2-www.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemd		
		
		

		
[www@localhost ~]$ sudo systemctl enable pm2-www

[www@localhost ~]$ pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /home/www/.pm2/dump.pm2