Using PM2 with Nodejs
Posted By : Kapil Bharadwaj | 03-Aug-2017
PM2:
Before start using pm2,you should know what is it and why is it so useful?
PM2 is a process manager for Node.js.it is very helpful tool to manage Node.js applications in production environment.there are many other process managers also like forever but PM2 stand out from them because of its features like deployment system and monitoring,declarative application configuration and automatic application load balancing.in simple terms It allows developer or devOps in keeping their applications alive forever,in reloading them without downtime and in facilitating common system admin tasks.
Install PM2:
you can install PM2 using NPM where NPM is package manager which was installed when you installed Node.Js.it is used to install all the packages and libraries to use with node.js.
# install pm2 by typing following command in your command prompt:
$ sudo npm install pm2 -g
now you have installed pm2 and ready to use it with your node.js application.
now i am assuming that your node.js application's entry point is app.js.
#To start your application with pm2, run it using following command:
$ pm2 start app.js
if you have defined different environments in your node.js application then you can also start it for different environments.
# mention the ENV before pm2:
$ NODE_ENV=production pm2 start app.js
after entering the command, you will see the report something like below:
where adityakumar is the user name. in your report, all the values will different according to your application.
Thats it now you have successfully started your node.js application with PM2.
now if Your application crashes, it will restart and it will keep a log on unhandled exceptions.
Moreover you can check the status of your application,check the error logs and stop the process also.
# To check the status,type the command:
$ pm2 status
it will give you the detailed report similar to the one which you got after starting it.
#To get the detailed logs you can go to the Directory home/adityakumar/.pm2/logs or You can type the command:
$ pm2 log 0
where 0 is the id which you get when you started or check status of pm2.
#To Stop the pm2, type the command:
$pm2 stop 0
where 0 is the id of the app which you get after starting pm2.
# To list all the running processes, type the command:
$ pm2 list
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Kapil Bharadwaj
Kapil is a Node Js Developer.