2

I am trying to have a nodejs application start automatically on system boot. Basically all I need is to run the command node /dir/app. I am using openwrt on an Arduino Yun. And have tried a couple things.

On the openwrt website it said I can do this. https://wiki.openwrt.org/inbox/procd-init-scripts :

#!/bin/sh /etc/rc.common
USE_PROCD=1
start_service() {
  procd_open_instance
  procd_set_param command node ///www/www-blink.js
  procd_close_instance
}

I have also tried changing the dir to /www/www-blink.js not ///

However i'm not sure what i'm doing wrong as nothing comes up when I try run it with /etc/init.d/node-app start I am obviously writing the code wrong but i'm not sure what it should exactly look like.

The other thing I have tried is the node modules forever and forever-service. I downloaded them on my computer using npm install -g forever and forever-service aswell. I transfered them to usr/lib/node_modules on my arduino yun. However when I try to use and forever(-service) commands it says

-ash: forever: not found

I have tried a couple other things, however nothing has worked. Any help would be greatly appreciated.

-- I also need to be able to start my express script with npm start not node app but I guess the first thing is getting it to work at all.

Manu Masson
  • 1,667
  • 3
  • 18
  • 37
  • http://stackoverflow.com/questions/11275870/how-can-i-automatically-start-a-node-js-application-in-amazon-linux-ami-on-aws – Alok Deshwal Mar 15 '16 at 09:02

4 Answers4

2

you can put the starting command (node /dir/app &)in the /etc/rc.local script. This will start your nodejs application automatically on system boot.

senox
  • 38
  • 1
  • 6
1

OpenWRT procd has a "respawn" parameter, which will restart a service that exits or crashes.

# respawn automatically if something died, be careful if you have an 
# alternative process supervisor if process dies sooner than respawn_threshold,
# it is considered crashed and after 5 retries the service is stopped

procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}

So, you cold just add: procd_set_param respawn 60 5 5

or something like that to your OpenWRT procd initscript. This 60 5 5 means it will wait 5s between respawns (middle parameter), and if it respanws more than 5 times (last parameter) in 60s (first parameter), it will disable the service ("restart loop" detected).

Refer to this page for more information: https://openwrt.org/docs/guide-developer/procd-init-scripts

anonymous3
  • 11
  • 2
0

You need to execute your node application like a Linux Service.

Upstart is perfect for this task

Upstart is an event-based replacement for the /sbin/init daemon which handles starting of tasks and services during boot, stopping them during shutdown and supervising them while the system is running.

If you have an app like this (for example):

// app.js
var express = require('express')  
var app = express()  
var port = process.env.PORT

app.get('/', function(req, res) {  
  res.send('Hello world!')
})

app.listen(port)

With a package.json like this:

{
  "name": "my-awesome-app",
  "version": "1.0.0",
  "dependencies": {
     "express": "^4.13.3"
  },
  "scripts": {
     "start": "node app.js"
  }
}

We create a upstart configuration file called myAwesomeApp.conf with the following code:

start on runlevel [2345]  
stop on runlevel [!2345]

respawn  
respawn limit 10 5

setuid ubuntu  
chdir /opt/myAwesomeApp.conf

env PORT=3000

exec npm start 

To finish, put your application (app.js and package.json) in the /opt/myAwesomeApp.conf and copy the configuration file myAwesomeApp.conf in /etc/init/

This is all, now you just need to run service myAwesomeApp start to run your node application as a service

emilioriosvz
  • 1,629
  • 1
  • 19
  • 30
0

I've never used procd before, but it likely needs the full path to node (e.g., /usr/bin/node). You'd need to make the line something like procd_set_param command /usr/bin/node /www/www-blink.js, assuming the file you want to run is /www/www-blink.js. You can locate node by running which node or type -a node.

Zenexer
  • 18,788
  • 9
  • 71
  • 77
  • Tried this. However it still said nothing or showed no processes when I used `top`. I used `/etc/init.d/node-app start` and also tried `/etc/init.d/node-app enable` with a `START=1` – Manu Masson Mar 27 '16 at 09:09