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)
#!/bin/sh #/etc/init.d/myService export PATH=$PATH:/usr/local/bin export NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules case "$1" in start) exec forever --sourceDir=/usr/local/bin/server -p /usr/local/bin/server myNodeApp.js #scriptarguments ;; stop) exec forever stop --sourceDir=/usr/local/bin/server myNodeApp.js ;; *) echo "Usage: /etc/init.d/myService {start|stop}" exit 1 ;; esac 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...
10 Comments
Tip 3 years ago on Introduction
use pm2. it's much simpler than all this. it's designed specifically for running node.js processes and can run them at startup, reboot, keep log files of stdout and stderr, etc.
4 years ago
Cool this works. How about always showing the console. I would like to watch what request come in.
4 years ago
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
5 years ago
Thanks a million. This works perfectly.
5 years ago
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 &
5 years ago
You'll need to run
sudo npm install forever -g
In order to let the forever command work.
6 years ago
Thanks!
Gave me exactly what I needed. Quick and clean
6 years ago
I think I don't understand, where is the node command which executes your script? I mean 'node myNodeApp.js'
Reply 6 years ago
ups, now I understand. 'forever' is an alternative to 'node' command. I thought it was a thing from the OS.
Thanks for this!
7 years ago
This is so cool! I need to get back into programming.