PM2 is a production process manager for Node.js applications with a built-in load balancer. It helps keep applications alive forever, reload them without downtime, and facilitate common system admin tasks. This is similar to running a command as a process daemon. A lot like running...
Think of it as a process manager like sysVinit or systemd. Except you don't have to know how to write rc.init files or systemd.service files.
It requires something called npm in order to install it. For those who aren't familiar with npm, it's a module installer for nodejs. I feel like I need to get into a series of explaining.. "what is..". Nodejs is a jascipt server. Typically used as a javascript backend for other web servers (nginx, httpd, apache, etc..) but can be used a standalone web server as well. However we don't need it to be a javascript or webserver for the sake of this article.
Depending on your distro, npm sometimes comes as part of nodejs. For other distros you can install it separately. Think of npm like pip for python. At a higher level, it's a little like apt, dnf or pacman except it's only for nodejs modules.
OK, so now that I have npm installed, how do I install pm2?
The "-g" means make it globally available for all users. This is how you can check the status of pm2.
You wouldn't want to use this for a "run once" script. This is for something you want to be running all the time in the background.
OK, so how do we add a job to pm2?
Change "app.js" to the name of your bash shell script, or javascript, or python file. Pretty much any language will work.
It also works with process watching.
How can I check the status on my job?
How can I remove a job in pm2 if I don't need it anymore?
Here are a few examples of different ways to install jobs.
With python.
With bash.
With an environmental variable.
For commands that have option flags.
Hopefully this is enough to get you started with a new way to run applications in the background.
Happy Linux'ing!
Code:
nohup mycommand.sh &
Think of it as a process manager like sysVinit or systemd. Except you don't have to know how to write rc.init files or systemd.service files.
It requires something called npm in order to install it. For those who aren't familiar with npm, it's a module installer for nodejs. I feel like I need to get into a series of explaining.. "what is..". Nodejs is a jascipt server. Typically used as a javascript backend for other web servers (nginx, httpd, apache, etc..) but can be used a standalone web server as well. However we don't need it to be a javascript or webserver for the sake of this article.
Depending on your distro, npm sometimes comes as part of nodejs. For other distros you can install it separately. Think of npm like pip for python. At a higher level, it's a little like apt, dnf or pacman except it's only for nodejs modules.
OK, so now that I have npm installed, how do I install pm2?
Code:
npm install pm2 -g
The "-g" means make it globally available for all users. This is how you can check the status of pm2.
Code:
pm2 status
You wouldn't want to use this for a "run once" script. This is for something you want to be running all the time in the background.
OK, so how do we add a job to pm2?
Code:
pm2 start app.js
Change "app.js" to the name of your bash shell script, or javascript, or python file. Pretty much any language will work.
It also works with process watching.
Code:
pm2 start app.js --name myApp --watch
How can I check the status on my job?
Code:
pm2 show myApp
How can I remove a job in pm2 if I don't need it anymore?
Code:
pm2 delete myApp
Here are a few examples of different ways to install jobs.
Code:
pm2 start app.js
With python.
Code:
pm2 start script.py --interpreter python
With bash.
Code:
pm2 start script.sh --interpreter bash
With an environmental variable.
Code:
pm2 start app.js --name myApp --env production
For commands that have option flags.
Code:
pm2 start npm --name "awx-run" -- run awx
Hopefully this is enough to get you started with a new way to run applications in the background.
Happy Linux'ing!
