Process Manager 2 (aka PM2) is one of my favorite apps that allows me to manage my scripts. It is easy to use and extremely powerful.
What PM2 offers:
- Restart scripts automatically upon failure
- Restart scripts automatically upon boot
- Log management
- Easy Monitoring
- Watch Dog Support
- Cluster & Scaling Support
- Cross platform
- Easy to use
Why use PM2?
Many people use screen & tmux to run scripts on Linux and keep them running when you exit your SSH terminal. While these are both very useful for scripts you will be watching frequently or interacting with, it is not ideal for more than one or two apps or automatically handling failures.
PM2 will auto-detect if a script has quit and will automatically restart it. This is really handy for automatically handling rare exceptions and extremely powerful if your scripts are able to auto-resume.
How to install PM2
PM2 is a NodeJS application and requires NodeJS to be installed. While it was designed to manage NodeJS applications it supports Python and Ruby as well. In fact, I mostly use it to run Python scripts.
You will first need to install NodeJS and I highly recommend using Node Version Manager (nvm).
Install Node Version Manager
Installing nvm is really simple, and you can see the instructions on their Github Page.
It's is just this one line:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
After you install it though, you need to exit your SSH session and log back in.
Finally, you need to install a version of NodeJS, you have two options here. You can install the latest version of node (my preference) or you can install the latest Long Term Support (lts) release. The LTS release a bit further back and considered the production supported release.
Install latest NodeJS
nvm install node
nvm use node
Install LTS release of NodeJS
nvm install --lts
nvm use --lts
Regardless of your choice, you can check the version you have installed by running node -v
.
Install PM2 Module
Installing PM2 is real easy, just run npm install -g pm2
This will install PM2 as a global module. NodeJS modules are typically installed locally to the app you are developing, but since pm2 is a tool you use system-wide you want to have it installed globally (-g option).
Once installed you can just type pm2 ls
to see all running process. At this point, you won't have anything.
You can now use PM2 to run your scripts and keep an eye on them.
Start a script with PM2
To start a script you just need to type pm2 start myscript.js --name myscript
If you type pm2 ls
you will see the script running and some details about memory usage, how many restarts, the pid, and cpu usage.
If you want the script to run automatically at bootup, you will need to type pm2 save
every time you add or remove a script. This will tell PM2 to save the state of your scripts and recover that state upon reboot.
You can log out of SSH at this point and your script will continue to run, will be restarted if it fails, and will automatically start upon reboots.
Stop, restart, or remove a script
There are a few commands you will want to know to manage your scripts.
pm2 stop myscript
This command will stop the script called myscript. Keep in mind, you use the name you specified with the --name argument and not the file name.
pm2 restart myscript
This will restart the script nicknamed myscript.
You can also pm2 restart all
to restart all scripts or stop them all with pm2 stop all
.
If you ever want to see the current status of your scripts, you can use pm2 ls
or pm2 monit
.
Monit is a more detailed view and generally is used as a "tv mode" type of display.
If you need to remove a script (this stops the script and no longer manages it) you can use pm2 delete myscript
.
Logging
PM2 will automatically log the output of your scripts to a file. This is one of the best features of PM2. Your quick and dirty scripts don't need to manage logging to the screen and to a log file, PM2 can do this automatically.
You can view logs for your script by typing pm2 logs myscript
or view all logs at once using pm2 logs
.
You can also look at the raw log files in ~/.pm2/logs
Conclusion
This is enough to handle 90% of the functionality you need to take advantage of PM2. PM2 can do so much more and is far more powerful than the ease of use makes it appear.
If your script needs environment variables, command-line arguments, or you are having problems with Python buffering output, I recommend you check out the page on Eco System Files.
You can also check out the Clustering Docs if you plan on having it manage multiple instances and scaling of your app.
There is a web interface to PM2 that shows far more detail about your applications.
There is also a paid version you can read about on https://pm2.io that has more advanced features like Machine Learning Error Detection.
It's nice and all, but I prefer the simplicity of forever for most of what I do.
@themarkymark I love pm2. I use it on my Spee.ch server and my onelovedtube server. Recently I started using the "cron_restart" command in pm2 for restarting my leaky apps once a day so they dont run out of memory. It was introduced into pm2 a few versions ago.
Great app, glad to see it's used by yourself as well.
pm2 is deceptively easy to use but has so many features you would never expect. A real shame there it is so expensive to use the paid version. For $80/months should be unlimited processes or a lot more than just 24.
@themarkymark oh.. I was wondering what you got with the paid version. My servers are quite light weight and I have a max of three or four pm2 processes running. But by the looks of your screenshots you may be close to that limit.
Yeah seems kinda crappy of them to do that, in the linux community.
Posted using Partiko Android
I don't mind a paid version, it's just expensive with a very small amount of processes you can run compared to unlimited with the free version.
One of the biggest things with the paid version is Machine Learning Error detection. In a production environment, this is huge.
You can see the differences here:
@themarkymark oh the machine learning part sounds interesting. I'll need to read up on that.
Posted using Partiko Android
A lot of Enterprise IT systems are integrating machine learning to analyze and detect failures. The ELK stack for log management has some really cool machine learning in the paid versions.
Great article compilation!
Posted using Partiko Android
Thanks for showcasing this, I never heard of it before! Bookmarking for later use :^)
I was using tmux. PM2 looks like this is a better solution for me since my scripts tend to fail a lot. I really like the auto-restart feature.
Just to clarify, does
pm2 save
work as an auto-restart when script stops working due to various errors?pm2 save
saves the current state of your scripts. You might have some scripts handled under pm2 but stopped and some running. It will keep track of that for you and upon reboot, they will be in the same state.pm2 save
has nothing to do with auto-restart, that will happen automatically after doing apm2 start
I use tmux a lot as well for single processes. It's a shame it isn't as easy to log as screen but I like it far better.
cheers m8, thanks a lot for the commands.
👍
~Smartsteem Curation Team
Ahhhhh, I could call terminal commands from the code itself to use pm2. Nice idea, thanks. That solves the api issue.