Previously, I wrote about running Node.js apps with Nodemon and Forever but nowdays I’m using the ever so slightly more professional PM2.
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, the application crashes in the middle of the night and you find about it only in the morning – ah the horror. PM2 solves this by:
- allowing you to keep applications alive forever
- reloading applications without downtime
- facilitating common system admin tasks
To install PM2, run the following command:
sudo npm install pm2 -g
To start your process with PM2, run the following command (once in the root of your application):
pm2 start server.js
As you can see from the output shown on the image below, 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.
![PM2](/KB/Nodejs/1005425/PM2.jpg)
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). The command to do that is the following:
pm2 startup ubuntu
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:
sudo env PATH=$PATH:/usr/local/bin pm2 startup ubuntu -u nikola
I’m an engineer at heart and a jack of all trades kind of guy.
For those who care about titles, I hold a masters degree in computing from
FER (and a black belt in karate, but that’s another story…).
During the last years, worked in a betting software industry where I made use of my knowledge in areas ranging from full-stack (web & desktop) development to game development through Linux and database administration and use of various languages (C#, PHP, JavaScript to name just a few).
Currently, I’m a senior software engineer at
TelTech, where we make innovative communications apps, and I <3 it.
Lately, I’m very passionate about Ionic framework and am currently in the top 3 answerers on
StackOverflow in
Ionic framework. I wrote a book about Ionic framework which you can get for free on Leanpub:
Ionic framework – step by step from idea through prototyping to the app stores.
Other technical writing:
+ wrote a book
Getting MEAN with MEMEs
was a technical reviewer for a book
Deploying Node published by Packt
was a technical reviewer for a book
Getting started with Ionic published by Packt
After writing 300 posts, this is why I think
you should start blogging too
Come and see what I write about on
my blog.