How to install and configure Forever as a service to run Node.js application – Ubuntu 12.04

Forever is a beautiful tool to running Node.js process with monitoring. It will help you to keep up and running your Node.js application by restarting it if it failed.  In this article I will show you how to install Forever and configure it as a service to keep running on machine reboot.

How to install Node.js and NPM:

How to install Forever:

Some basic commands for Forever:

Now we will create our init script to run forever as a service:

Init Script:

Create a script in /etc/init.d/ folder named “mynodejsapplication”.

Append the below lines to the file. Please change the Application name from the script as per your requirement. You can also change the node path if you have any other path.

Now we will set execute permission to the script.

Now we have to update the system service definitions:

Now verify your script.

It will start your Node.js application. Now reboot your system and have fun with your new service.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
The following two tabs change content below.

Tapas Mishra

Sr. Engineer (DevOps)
Loves to work on Opensource products. Having experience on Linux environment. Knowledge on Public cloud services like AWS, Rackspace, DigitalOcean, Linode. Please don't hesitate to give a comment on the posts. Your comments are my strength.

5 thoughts on “How to install and configure Forever as a service to run Node.js application – Ubuntu 12.04

  1. Nice one, i love this site, lean more things from this. can you help about ‘How to install LAMP on Ubuntu 13.10’? i try a lot to install lamp on Ubuntu 13.10 but no success.

    Thanks Sanjib

  2. Hi, thanks for the data!

    But it did not work for me (forever v0.11.1 and forever-monitor v1.2.3).

    The script is able to start the process but is not creating any pid file, looking at the forever github, it seem to be a bug and you need to add the “-p” among with the others param in order to create the file.

    And in the stop option if you delete the pid file, forever monitor will re-launch another process since “he thinks” that the nodejs went down, i solved this using “forever stop [uid]” instead of “rm -f $PIDFILE” (you can set –uid on the start script ) this cmd also delete the pid file.

    Again this is an really nice work to get stared with init.d scripts and my personal case; just want to share it.

    Thanks

  3. Agree with Elias, stop needs to use forever stop instead rm -f.
    I changed that line with this and now works without problems

    forever stop $APPLICATION_START

Leave a Reply