Keeping it running

Not only do we want the application to run when we log out, we want our application to keep running reliably. The production servers are not frequently restarted, and in general we will like to ensure that they come back up as soon as possible when there is a crash, a failure, or an error. For node, generally it means restarting the process as soon as it fails. There are many ways to keep the node server running. In this section we will see two of them:

  • Monit
  • Forever

Here is how Monit is described on its website (http://mmonit.com/monit/):

Monit is a free open source utility for managing and monitoring processes, programs, files, directories, and filesystems on a UNIX system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations.

Let us begin with installing Monit. On RPM-based or Yum-based systems such as RHEL, Fedora, or CentOS, you can install it using the yum command, as shown here:

$ sudo yum install monit

Or on a Debian- or apt-get-based system, you can install Monit using apt-get:

$ apt-get install monit

For other systems, you can check the installation instructions at the Monit website.

Once Monit is installed, we can configure it to manage our node application. For this, we will create a configuration file (in our case we will call it awesome-chat) in /etc/monit.d/ or /etc/monit/conf.d/, depending on your Monit installation:

check host objdump with address 127.0.0.1
    start program = "/bin/sh -c 
    'NODE_ENV=production 
    node /opt/node_apps/awesome-chat/app.js 2>&1 
    >> /var/log/awesome-chat.log'"
        as uid nobody and gid nobody
    stop program  = "/usr/bin/pkill -f 
  'node /opt/node_apps/awesome-chat/app.js'"
    if failed port 3000 protocol HTTP
        request /
        with timeout 10 seconds
        then restart

In this file, you should notice the highlighted section. We are emphasizing the program or more importantly, the commands to start/stop our application and then finally configuring Monit to restart the application in case of a failure. This is detected by sending an HTTP request to fetch the page at port 3000.

That is it; we can start our application with the following command:

$ monit start awesome-chat

And stop it with the following code:

$ monit stop awesome-chat

In case of a crash, Monit will take care of restarting the application.

Monit can be used to run and watch any daemon service. It also has a web interface in case you want to check the status, which by default runs on port 2812. You can learn more about Monit on its website and in its manual online.

Another, more node-specific way to keep our server up and running is Forever (https://github.com/nodejitsu/forever). Forever describes itself as:

A simple CLI tool for ensuring that a given script runs continuously.

And that's what is does. Given your node application script, Forever will start it and make sure it keeps running continuously. Since Forever itself is a node application, we will use npm to install it:

 $ sudo npm install forever -g

Now, to start the application with Forever, it is just a matter of executing the app.js file with forever. Just run the following command:

$ forever start app.js

We can see the list of applications running forever with the following command:

$ forever list
   0 app.js [ 24597, 24596 ]

To stop the application, use the forever stop command:

$ forever stop 0

Visit Forever's github page for understanding more about Forever and its workings.

There are several other tools on *nix systems to make node run as a daemon. Few of them are as follows

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset