Informing the IBM Community

Keep My NodeJS App Running!


In my previous article, we created a quick weather app running on our favourite server.

In this article, I will show you how we can ensure this application is always up and running, and if it’s not running, get it to automatically restart.
Node applications are different to say, PHP applications, where we have the Apache HTTP webserver that runs our applications.  If the PHP application is not running, we restart the HTTP server using the Start TCP/IP Server command, as shown below.

Well, that is fine for PHP applications, but what about NodeJS?  Those that have seen my iUG presentations and videos will know that I often open a Node presentation with, Node is Weird!

That is wrong really, Node is not weird, it is just different – I will leave it as that!

With Node we don’t use Apache – ah I hear you say, yes you can – okay then, you can run NodeJS under Apache, but not without being a bit of an Apache expert and knowing all about reverse proxy and virtual ports.

Going back to our weather app, if we want it to always be available to our users, we have to have a PASE session running our application, as soon as we close the PASE session, our application is not available to our users.

Below we can see I have started the weather app in a Windows Terminal session. 

Now, if we close that terminal window, our app will close, and the users will no longer have access. 

By running our app here, this is known as a process and luckily for us, there are utilities called process managers.  I will be showing you one called PM2. 

What is PM2?

PM2 is a NodeJS process manager, which helps us manage Node.js applications in a production environment. 

It is an Open Source program, and with 500,000 weekly downloads, it is well supported.  It is popular for a very good reason – its impressive!

Installing PM2

We use NPM to install PM2.  In a PASE shell (try using Windows Terminal – see my previous article type the command npm install pm2 -g

Using the -g flag will install PM2 globally, so it is available to all our applications. 

I am not one for recommending installing open source packages globally, but I think its fully justified in this instance.  Maybe, a future article on my thoughts on this topic.

PM2 Monitoring

PM2 has some great tools for monitoring.  Let me show you some of them.

To check what PM2 is monitoring, we use the pm2 ls command.  This is shown in the figure below.

In this example we have five applications all running.

We seem to be having a problem with one of these applications, the f_MTD_Dev application. 

By firing up the PM2 monitor, PM2 monit, then moving down to this application, we can see the logs for this, and it is highlighting that there is a node module missing from this application. 

Who was supposed to be testing this, back to the developers!

To check the logs of any of the applications being monitored, we could have used the PM2 logs 1 command to show the errors on f_MTD_Dev.

Adding Weather App

If we wish to add the weather application we developed in the last issue, all we need to do is run the command in our PASE shell;

pm2 start /powerwire/weather/bin/www –name ‘PowerWire Weather’

Where /powerwire/weather/bin/www is the name of the node script to start our application and
–name ‘our app name’ is the name to give PM2 to use.

PM2 will now start our application in the background, it does not matter if we close the shell window.

That is it, so easy to add new applications to the PM2 process.


To ensure that all our NodeJS applications are always running, we have put a shell script together that gets run when our server is IPL’d. 

This script is shown below.

I have highlighted the new addition to the script.  Change to the weather directory and run the PM2 start command.

This script has to be executed in our start-up program by using the command

QSH CMD(‘/startup/’)

All our NodeJS commands will now be up and running at start-up.

Checking for Changes

Using PM2, we can also check if there are any file changes in our application, and if there are, it reloads it to make sure it is always up to date.

To allow your app to use this feature we just add the –watch flag to PM2 start to make sure PM2 watches your files for changes.

Restarting an Application

Another great benefit of using PM2 is its ability to restart an application if it is not running. 

If I manually kill off an application, on a 5250 session, PM2 will restart it for me.  How neat it that!

It is difficult in an article to show you this process.  Maybe, a video on this will explain it better – standby, as they say in the Navy 😊.

PM2 Commands

For your reference, I have put common PM2 commands in the table below

A full reference can be found on the PM2 website


Using PM2, we can easily have our NodeJS applications monitored and readily available for our production users.

There are other process managers out there, but we have found that PM2 fits our need perfectly and have stuck with it, but beware, in reality, we are using NodeJS to monitor NodeJS.  I will leave that one with you!

If you have any questions, either on this article, or any other open source, use the comments below, or send me a message on twitter @AndyYouens

Andy Youens is an IBM i consultant/instructor at Milton Keynes, UK-based FormaServe Systems with over 40 years IBM midrange experience.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.


Leave a Reply

Your email address will not be published. Required fields are marked *