Node.js App As a RPI Service (boot at Startup)

Introduction: Node.js App As a RPI Service (boot at Startup)

I wanted to make a Node.js app/script I wrote, persistent among reboots, on my Raspberry Pi 2. Although Forever gives us the possibility of running a script continuously (on background), that doesn't include system reboots whether manual or by power failures...

After digging on the web with lots of info, i found a way that works for me, and I'm sharing it so that, hopefuly, it will help someone. On the PI, there's a directory ( /etc/init.d ) where some services are specified, as well as the commands and parameters to use them. That allows us to create a service to be called on every boot of the RPI.

Step 1: Service Specification

My Node.js app is called myNodeApp.js and lies on /usr/local/bin/server/ directory.

The first step, on terminal window, is change to the etc/init.d folder (as root)

$ cd /etc/init.d

Create a new file, using a text editor like 'nano'. In this case let's call the service we want to create "myService":

$ nano myService

The contents of the new file are these: (Note the 4th line where my particular Node.js instalation is specified)

export PATH=$PATH:/usr/local/bin
export NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules

case "$1" in
exec forever --sourceDir=/usr/local/bin/server -p /usr/local/bin/server myNodeApp.js  #scriptarguments
exec forever stop --sourceDir=/usr/local/bin/server myNodeApp.js
echo "Usage: /etc/init.d/myService {start|stop}"
exit 1
exit 0

Step 2: How to Use the Newly Created Service

After creating the service on step1 we need to make it ready to run on boot:

The file we created is a text file, so we need to make it executable with the following command:

chmod 755 /etc/init.d/myService 

Now we can test it:

sh /etc/init.d/myService start/stop

If all goes well we can, finally, make it bootable:

update-rc.d myService defaults

To remove it from boot:

update-rc.d -f myService remove

I have it running for a few months without any fault...Hope you enjoy...



    • Creative Misuse Contest

      Creative Misuse Contest
    • Metalworking Contest

      Metalworking Contest
    • Fix It! Contest

      Fix It! Contest

    9 Discussions


    5 months ago

    Cool this works. How about always showing the console. I would like to watch what request come in.

    Hey, It didn't work for me.. The service started on "sh /etc/init.d/myService start" and so I did "update-rc.d myService defaults". and I restarted the raspberry pi but it didn't work.

    FYI, I made a web service using express which runs on port 8080 and I am accessing it from the PC's web browser

    Thanks a million. This works perfectly.

    Hello guys. The service autorun approach leads to some problems with scripts consisted exception handling (after the exception fires, the process stops responding). As for me, the much more suitable method is just to add one string at the beginingof /etc/rc.local:

    node /full/path/to/myscript.js < /dev/null &

    You'll need to run

    sudo npm install forever -g

    In order to let the forever command work.


    Gave me exactly what I needed. Quick and clean


    1 year ago

    I think I don't understand, where is the node command which executes your script? I mean 'node myNodeApp.js'

    1 reply

    ups, now I understand. 'forever' is an alternative to 'node' command. I thought it was a thing from the OS.

    Thanks for this!

    This is so cool! I need to get back into programming.