{"id":1812,"date":"2015-06-18T04:23:29","date_gmt":"2015-06-18T04:23:29","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1812"},"modified":"2015-08-20T11:52:59","modified_gmt":"2015-08-20T11:52:59","slug":"using-pm2-to-run-your-node-js-apps-like-a-pro","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/javascript\/nodejs\/using-pm2-to-run-your-node-js-apps-like-a-pro\/","title":{"rendered":"Using PM2 to run your Node.js apps like a pro"},"content":{"rendered":"<p><em>Previously I wrote about <a href=\"http:\/\/www.nikola-breznjak.com\/blog\/nodejs\/keep-your-node-js-scripts-running-with-forever-and-nodemon\/\">running Node.js apps with Nodemon and Forever<\/a>\u00a0but nowdays I&#8217;m using the ever so slightly\u00a0more professional <a href=\"https:\/\/github.com\/Unitech\/pm2\">PM2<\/a>.<\/em><\/p>\n<p>Running your Node.js application by hand is, well, not the way we roll. Imagine restarting the app every time something happens, or god forbid application crashes in the middle of the night and you find about it only in the morning &#8211; ah the horror. <a href=\"https:\/\/github.com\/Unitech\/pm2\">PM2<\/a> solves this by:<\/p>\n<ul>\n<li>allowing you to keep applications alive forever<\/li>\n<li>reloading applications without downtime<\/li>\n<li>facilitating common system admin tasks<\/li>\n<\/ul>\n<p>To install PM2, run the following command:<\/p>\n<pre class=\"code-pre \"><code langs=\"\">sudo npm install pm2 -g<\/code><\/pre>\n<p>To start your process with PM2, run the following command (once in the root of your application):<\/p>\n<pre class=\"lang:default decode:true  \">pm2 start server.js<\/pre>\n<p>As you can see from the output shown on the image\u00a0below, PM2 automatically assigns an App name (based on the filename, without the .js extension) and a PM2 id. PM2 also maintains other information, such as the PID of the process, its current status, and memory usage.<\/p>\n<p><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/06\/PM2.jpg\" rel=\"lightbox[1812]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1777\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/06\/PM2.jpg\" alt=\"PM2\" width=\"677\" height=\"109\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/06\/PM2.jpg 677w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2015\/06\/PM2-300x48.jpg 300w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><\/a><\/p>\n<p>As I mentioned before, the application running under PM2 will be restarted automatically if the application crashes or is killed, but an additional step needs to be taken to get the application to launch on system startup (boot or reboot).\u00a0The command to do that is the following:<\/p>\n<pre class=\"lang:default decode:true \">pm2 startup ubuntu<\/pre>\n<p>The output of this command will instruct you to execute an additional command which will enable the actual startup on boot or reboot. In my case the note for the additional command was:<\/p>\n<pre class=\"lang:default decode:true \">sudo env PATH=$PATH:\/usr\/local\/bin pm2 startup ubuntu -u nikola<\/pre>\n<p>If you want to learn more about the additional PM2 options you can take a look at <a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04#other-pm2-usage-(optional)?refcode=974c9bc93d77\">this post<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previously I wrote about running Node.js apps with Nodemon and Forever\u00a0but nowdays I&#8217;m using the ever so slightly\u00a0more professional PM2. Running your Node.js application by hand is, well,&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1864,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,4],"tags":[],"class_list":["post-1812","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-codeproject","category-nodejs"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1812"}],"version-history":[{"count":4,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1812\/revisions"}],"predecessor-version":[{"id":2047,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1812\/revisions\/2047"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/1864"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}