Node.js script at startup
I've been searching a while how to run a Node.js program at the boot of my Raspberry Pi and only one technique worked for me. Moreover I found it very simple!
forever and forever-service
Basically, what you have to do is make your program a service and then run the service automatically at the startup.
Install forever as global :
1 |
npm install forever -g |
Then install forever-service : the key program here!
1 |
npm install forever-service -g |
This is the github link: https://github.com/zapty/forever-service
Then, you HAVE TO rename your program to app.js. Lets say we want our service to be called myService, we run this command in the same directory than app.js :
1 |
forever-service install myService |
If everything went right, you are now able to start|stop|restart your service like this:
1 2 3 |
service myService start service myService stop service myService restart |
Run at startup
To run the service at the startup, open the file /etc/rc.local :
1 |
sudo nano /etc/rc.local |
And add to the end (but before exit 0) the following line:
1 |
sudo service myService start |
sudo is important here!
This is it you can reboot your server, and the service should run!