pm2, node的程序(進程)管理工具, 使用與教學

今天的東東叫pm2, pm2是node的一個可監控程序的工具, 不多廢, 進主題

進行全域安裝
workspace npm install pm2 -g                                                                                                                           
/home/cabox/.nvm/versions/node/v5.2.0/bin/pm2 -> /home/cabox/.nvm/versions/node/v5.2.0/lib/node_modules/pm2/bin/pm2                                       
/home/cabox/.nvm/versions/node/v5.2.0/bin/pm2-dev -> /home/cabox/.nvm/versions/node/v5.2.0/lib/node_modules/pm2/bin/pm2-dev                               
/home/cabox/.nvm/versions/node/v5.2.0/bin/pm2-docker -> /home/cabox/.nvm/versions/node/v5.2.0/lib/node_modules/pm2/bin/pm2-docker                         
/home/cabox/.nvm/versions/node/v5.2.0/bin/pm2-daemon -> /home/cabox/.nvm/versions/node/v5.2.0/lib/node_modules/pm2/bin/pm2-daemon                         
/home/cabox/.nvm/versions/node/v5.2.0/bin/pm2-runtime -> /home/cabox/.nvm/versions/node/v5.2.0/lib/node_modules/pm2/bin/pm2-runtime                       
/home/cabox/.nvm/versions/node/v5.2.0/lib                                                                                                                 
`-- pm2@2.5.0                                                                                                                                             
                                                                                                                                                          
npm WARN optional Skipping failed optional dependency /pm2/chokidar/fsevents:                                                                             
npm WARN notsup Not compatible with your operating system or architecture: fsevents@1.1.2                                                                 
➜  workspace          
YA裝好了, 目前安裝的是2.5.0版本

如何使用它
進入到你的node app資夾, 一般情況下都是執行npm start啟動你的node app
start的內容多半是指定在package.json裡, 可以去看就道了, 如果你的啟動是index.js
那就是...
bk git:(master) pm2 start index.js                                                                                                                     
[PM2] Starting /home/cabox/workspace/bk/index.js in fork_mode (1 instance)                                                                                
[PM2] Done.                                                                                                                                               
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐                                                                  
 App name  id  mode  pid   status  restart  uptime  cpu  mem        watching                                                                   
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤                                                                  
 index     0   fork  9660  online  0        0s      0%   13.3 MB    disabled                                                                   
└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────┴───────────┴──────────┘                                                                  
 Use `pm2 show ` to get more details about an app  
瞧, 有了! 這樣就開始被監控了, 然後你的app 也啟動了

也是可以將App name命名, 如
bk git:(master) pm2 start index.js --name myapp                                                                                                        
[PM2] Spawning PM2 daemon with pm2_home=/home/cabox/.pm2                                                                                                  
[PM2] PM2 Successfully daemonized                                                                                                                         
[PM2] Starting /home/cabox/workspace/bk/index.js in fork_mode (1 instance)                                                                                
[PM2] Done.                                                                                                                                               
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐                                                                 
 App name  id  mode  pid    status  restart  uptime  cpu  mem        watching                                                                  
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤                                                                 
 myapp     0   fork  10014  online  0        0s      0%   13.4 MB    disabled                                                                  
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────────┘                                                                 
 Use `pm2 show ` to get more details about an app 
瞧, 有了! 己變成myapp, 然後你的app 也啟動了, 去看看吧
(app啟動無圖) XD

接下來看看監控的dashboard
bk git:(master) pm2 monit       
會出現下列
Process list ─────────────────────────────┐┌─ Global Logs ───────────────────────────────────────────────────────────────────────────────────────────┐ 
[ 0] myapp     Mem:  44 MB    CPU:  0 %  on │ myapp > Executing (default): SELECT `id`, `title`, `body`, `noteday`, `createdAt`, `updatedAt`,         │ 
                                            │ `ProfileId` FROM `Notes` AS `Note`;                                                                     │ 
                                            │ myapp > Executing (default): SELECT `id`, `email`, `password`, `createdAt`, `updatedAt` FROM            │ 
                                            │ `Accounts` AS `Account`;                                                                                │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
                                            │                                                                                                         │ 
└────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘ 
┌─ Custom metrics (http://bit.ly/code-metric─┐┌─ Metadata ──────────────────────────────────────────────────────────────────────────────────────────────┐ 
│ Loop delay                         0.47ms  ││ App Name              myapp                                                                             │ 
│                                            ││ Restarts              0                                                                                 │ 
│                                            ││ Uptime                14m                                                                               │ 
│                                            ││ Script path           /home/cabox/workspace/bk/index.js                                                 │ 
│                                            ││ Script args           N/A                                                                               │ 
│                                            ││ Interpreter           node                                                                              │ 
│                                            ││ Interpreter args      N/A                                                                               │ 
│                                            ││ Exec mode             fork                                                                              │ 
│                                            ││ Node.js version       5.2.0                                                                             │ 
│                                            ││ watch & reload        ?                                                                                 │ 
└────────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘ 
瞧, 是不是, 監控畫面開啟了

如何停止它
workspace pm2 stop myapp                                                                                                                               
[PM2] Applying action stopProcessId on app [myapp](ids: 0)                                                                                                
[PM2] [myapp](0) ✓                                                                                                                                        
┌──────────┬────┬──────┬───────┬─────────┬─────────┬────────┬─────┬───────────┬──────────┐                                                                
 App name  id  mode  pid    status   restart  uptime  cpu  mem        watching                                                                 
├──────────┼────┼──────┼───────┼─────────┼─────────┼────────┼─────┼───────────┼──────────┤                                                                
 myapp     0   fork  0      stopped  0        0       0%   0 B        disabled                                                                 
 myapp2    1   fork  10826  online   0        2m      0%   24.2 MB    disabled                                                                 
└──────────┴────┴──────┴───────┴─────────┴─────────┴────────┴─────┴───────────┴──────────┘                                                                
 Use `pm2 show ` to get more details about an app                                 
瞧, myapp己停止了

如何刪除它
workspace pm2 delete myapp                                                                                                                             
[PM2] Applying action deleteProcessId on app [myapp](ids: 0)                                                                                              
[PM2] [myapp](0) ✓                                                                                                                                        
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐                                                                 
 App name  id  mode  pid    status  restart  uptime  cpu  mem        watching                                                                  
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤                                                                 
 myapp2    1   fork  10826  online  0        11m     0%   22.7 MB    disabled                                                                  
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────────┘                                                                 
 Use `pm2 show ` to get more details about an app       
瞧, myapp己被刪了

不再用它時

好了, 關閉pm2的監控吧
bk git:(master) pm2 kill                                                                                                                               
[PM2] Stopping PM2...                                                                                                                                     
[PM2] Applying action deleteProcessId on app [all](ids: 0)                                                                                                
[PM2] [myapp](0) ✓                                                                                                                                        
[PM2] All processes have been stopped and deleted                                                                                                         
[PM2] PM2 stopped                                                 

workspace pm2 list                                                                                                                                     
[PM2] Spawning PM2 daemon with pm2_home=/home/cabox/.pm2                                                                                                  
[PM2] PM2 Successfully daemonized                                                                                                                         
┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬─────┬──────────┐                                                                         
 App name  id  mode  pid  status  restart  uptime  cpu  mem  watching                                                                          
└──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴─────┴──────────┘                                                                         
 Use `pm2 show ` to get more details about an app              
瞧, 己經沒有app被監控了

收工, 今天就介紹到這兒吧, 下次再做進階的應用, 881~





沒有留言: