NODEJS(7)Nodes Live for Ever
NODEJS(7)Nodes Live for Ever1. Installation PM2 Tool
>sudo npm install pm2@latest -g
2. How we use pm2
Command to start multiple nodes on one machine with one port number
>PORT=3001 DEBUG="express:* monk:*" pm2 start app.js --name noderest1 -i max
-i max will be use as much CPU resource as we can
Command to start multiple nodes on one machine with one port number
>PORT=3001 DEBUG="express:* monk:*" pm2 start app.js --name noderest1 -i 4
-i 4 means will start 4 apps on this port.
Command to list all the apps running on my machine.
>pm2 list
│ App name │ id │ mode │ PID │ status │ restarted │ uptime │ memory │ watching ├─────────────┼────┼─────────┼───────┼────────┼───────────┼────────┼──────────────┼────────────│ noderest1 │ 0 │cluster│ 2167 │online│ 0 │ 7d │ 124.000 KB │unactivated│ noderest1 │ 1 │cluster│ 2168 │online│ 0 │ 7d │ 784.000 KB │unactivated│ noderest1 │ 2 │cluster│ 2169 │online│ 0 │ 7d │ 2.512 MB │unactivated││ noderest1 │ 3 │cluster│ 2174 │online│ 0 │ 7d │ 18.137 MB │unactivated│ Pm2Http9615 │ 4 │fork │ 70410 │online│ 0 │ 4d │ 1.188 MB │unactivated│└─────────────┴────┴─────────┴───────┴────────┴───────────┴────────┴──────────────┴────────────
We can only get the JSON response from the servers.
>pm2 web
We can visit http://localhost:9615/ and get all the JSON data for the processes and machine information.
Monitor all the apps
>pm2 monit
⌬ PM2 monitoring : ●noderest1 [ ] 0 % [ ] 124.000 KB ●noderest1 [ ] 0 % [| ] 784.000 KB ●noderest1 [ ] 0 % [|||| ] 2.512 MB ●noderest1 [ ] 0 % [||| ] 19.148 MB ●Pm2Http9615 [ ] 0 % [|| ] 1.188 MB
Show all the logs
>pm2 logs
Empty all log files
>pm2 flush
PM2 Flushing /Users/carl/.pm2/pm2.log PM2 Flushing PM2 /Users/carl/.pm2/logs/noderest1-out-0.log PM2 /Users/carl/.pm2/logs/noderest1-err-0.log PM2 Flushing PM2 /Users/carl/.pm2/logs/noderest1-out-1.log PM2 /Users/carl/.pm2/logs/noderest1-err-1.log PM2 Flushing PM2 /Users/carl/.pm2/logs/noderest1-out-2.log PM2 /Users/carl/.pm2/logs/noderest1-err-2.log PM2 Flushing PM2 /Users/carl/.pm2/logs/noderest1-out-3.log PM2 /Users/carl/.pm2/logs/noderest1-err-3.log PM2 Flushing PM2 /Users/carl/.pm2/logs/Pm2Http9615-out-4.log PM2 /Users/carl/.pm2/logs/Pm2Http9615-err-4.log
And also some other useful commands.
pm2 stop 0, pm2 delete 0, pm2 delete all, pm2 stop all, pm2 restart all,
Start One App with Name, then manage it with Name
>PORT=3001 DEBUG="express:* monk:*" pm2 start app.js --name noderest1 -i max
>pm2 restart noderest1
Processing...... PM2 Restarting process by name noderest1 PM2 Process noderest1 restarted
Reloading without downtime
>pm2 reload noderest1
Processing...... PM2 Reloading process by name noderest1 PM2 Process noderest1 succesfully reloaded PM2 Process noderest1 succesfully reloaded PM2 Process noderest1 succesfully reloaded PM2 Process noderest1 succesfully reloaded PM2 All processes reloaded
pm2 can watch current directory and restart the app when a file changes
>pm2 start app.js —watch
References:
Make the nodejs app live for ever
https://github.com/nodejitsu/forever
https://github.com/Unitech/pm2
https://github.com/Unitech/pm2#a1
页:
[1]