Shih-Min Lee's Personal website

dating, chating, food, games, search

Follow me on GitHub

😀 pm2 and GNU screen monitoring

Abstract

pm2 is a processing logging and monitoring application. you can use it to monitor memory and cpu usages. It can also be configured to log the outputs to different transports. It’s really really cool.

pm2

Sometimes it’s not ok to run the processes in the background. Let’s say you want to run 3 processes through ssh at the same time. The traditional way to do it is open 3 terminals and ssh to the same ip address and start 3 processes separately. But you can also ssh into 1 terminal and run 3 processes in the backend. However, when the process is in the background it becomes hard to know what exactly is going on.

pm2 is a way to keep those processes alive and at the same time monitor those processes for you.

I tried to combine pm2 with screen so you can switch the screen easily using GNU screen.

$ sudo apt-get -y install pm2 screen

and then you can start using those services.

Things that I usually do

$ screen

this creates a screen session

inside a screen:

$ ctrl + a + c

this creates a new screen

$ ctrl + a + n

go to next screen

$ ctrl + a + d

detach from this screen

$ screen -r
or
$ screen -r xxxx

re-attach to the previous running screen process with id: xxxx

$ NODE_ENV=production PORT=8080 DEBUG=isomorphic500 pm2 start index.js
$ NODE_ENV=production pm2 start server.js

to start servers in many places

$ pm2 logs

you can monitor console logs real time, aggregated across processes

$ pm2 monit

monitor cpu and memory usage of all minitored processes

$ pm2 stop xxx

stop running pm2 process named xxx

$ pm2 restart xxx

restart running pm2 process named xxx

$ pm2 monit

starts a pm2 monitor

$ pm2 start server.js -i 0

Start your process on all of your cpu cores. Notice you’re not gonna get 8 times request per second if you have 8 cores because of some overheads.

references:

09 Aug 2015