What are process managers?
Process managers are software that brings ease in effectively managing processes on a server and taking server resources benefit without an interruption from an application code.Process managers help out with load balancing, Clustering, Resuscitation (reviving a dead worker process), multi-host deployment, an ability to control application life-cycle, monitor the running services and facilitate common system admin tasks to maintain your project operability. Runs as daemon, providing ability to start, restart and stop. This can be done efficiently using PM2 Process Manager.
Some Node Js process managers.
Some process managers that can be used with node js applications.
PM2 Process manager.
All the above mentioned process managers have their pros and cons, but PM2 turns out to be efficient and provides more configuration when compared to others and mostly because of its simplicity and easiness. Some are comparatively new, so they need to be explored thoroughly.
Pm2 is an advanced, production grade process manager. PM2 will take responsibility for managing applications on different processor cores without being worried about a master and worker headache. It helps to keep application independent of cores and processes management. It helps the application to re deploy with zero downtime. This is made sure by creating more than one instance of the application using clustering and gracefully deploy each instance thus achieving zero downtime.
Following are some main functionalities provided by PM2:
- Behavior configuration
- Container Integration
- Cluster Mode
- Watch & Reload
- Log management
- Startup Scripts
Pm2 provides a process file or an ecosystem file to provide configurations of different parameters that control the applications such as the remote machine user, SSH options, git repository, pre-setup scripts or commands, post-setup scripts or commands , pre-deploy scripts or commands and post-deploy scripts or commands. In this ecosystem file we can define different configurations for different environments such as production, development, staging or QA. The execution mode is also defined to be cluster or default (Fork) in this ecosystem file and the number of instances are also specified here. The ecosystem file can be generated in 4 formats namely, JS, JSON, YAML or JSON5. Pm2 can run any script file in any language just like “forever” service.A good competitor for PM2 is the new StrongLoop Process Manager.
What is PM2.
PM2 is a very vast and complex process manager providing many configurations and capabilities to manage applications, to completely utilize it’s potential the documentation is best but here we will see some basic commands to simply deploy an application.
- To install the PM2 process manager, it must be installed globally and can be installed using node package manager and nodeJS cli .
- Npm install [email protected] -g
- To start an application navigate to the directory containing the “server.js” or “app.js” using the Node JS Cli tool. And type in the following command. This will start the application on server.
- pm2 start app.js
- To get a list of the running servers, type in the command:
- detailed list “pm2 list”.
- JSON list “pm2 jlist”.
- Beautified JSON list “pm2 prettylist”.
- For Starting, Restarting or deleting and application from the list, following commands are used.
- Stop a Process “pm2 stop (pid/name)”.
- restart a process “pm2 restart (pid/name)”.
- Delete a process “pm2 delete (pid/name)”.
- For monitoring the performance of each process the following commands displays a monitor in the CLI.
- pm2 monit
- Using the following command we can create a process/ecosystem.config.js or process/ecosystem.json file.
- Pm2 ecosystem
This process file if in “JS” format will have name “ecosystem.config.js”. This file is used to set the environment variables and define configurations. Now we can start the application using this process file in the required environment.
- Pm2 start process.json/process.config.js –env <environment>
What you will need:
- A VPS (server) that you can SSH into.
- A Git repository.
The first thing to do is make sure both your computer and your server have Node.js and npm installed.
If you need to update PM2 on either your computer or the server, you can install the latest at any point using
- npm install -g [email protected]
and then the following command will update the in-memory PM2 to the latest installed version.
- pm2 updatePM2
Work on your local copy of your code and test/run locally until you’re satisfied. Once satisfied, push up to Git repository. Use PM2 to deploy your latest version from Git repository.
This means your server needs to have SSH access to the git repository, so that PM2 can pull your code and restart the server with the changes. We have already created the ecosystem.config.js/ecosystem.json file, now in deploy section, configure according to needs.
This allows you to have multiple deployment options based on your needs (dev, staging, production), all of which are managed with a single ecosystem file.
To setup the application for the first time, that is to run pre-setup or post setup commands, run the following command.
pm2 deploy ecosystem.json/ecosystem.config.js <environment> setup
Once this is done, go ahead and deploy application
pm2 deploy ecosystem.json/ecosystem.config.js <environment>
This will deploy your code, install all dependencies, and run your Node.js server.
Have questions ? Share with us coz we listen 🙂