7

I have an IoT project running on a Raspberry Pi 2 using the Raspbian Jessie OS.

It's a web server running in NodeJS (v4) and I'm using Winston to log to Loggly's logging service. All works well when the project is kicked off via npm start from terminal (when running as 'pi' or via sudo -s). However, when the project starts on boot, the logging doesn't work and I can't work out why.

To start the project on boot I created a etc/init.d script. The project starts and serves traffic, everything works great except for logging. I can't see any errors (though not having logging doesn't help). This is how I start my project from inside my etc/init.d script:

/usr/bin/node /var/www/curtains/server.js

I'm using winston: https://www.npmjs.com/package/winston and winston-loggly: https://www.npmjs.com/package/winston-loggly.

Any ideas why, when the process is started on bootup the logging doesn't work?

Adding winston initialization code as requested:

var winston = require('winston');
require('winston-loggly');

 winston.add(winston.transports.Loggly, {
    token: "<snip>",
    subdomain: "<snip>",
    tags: ["tag", ip.address()],
    json:true
});

winston.log('info',"Server.js starting up");
ConfusedNoob
  • 9,826
  • 14
  • 64
  • 85
  • Do you have any relative paths in your log configuration? Also it would be helpful to see your winston initialization from `server.js` can you add this code? – Kevin Burdett Feb 18 '16 at 00:52
  • Added initialization code from server.js - there is no log configuration that I'm aware of. – ConfusedNoob Feb 19 '16 at 17:36

1 Answers1

2

When you run npm start, node will look for a scripts object in your package.json file, and run the commands associated with the start key.

In your init.d script you are not running npm start, but instead just running node and passing your server.js file as the first argument (which will run that file).

Most likely, something in your start script is needed in order to run your logging correctly. To solve this, you can either:

  • In your init.d script, cwd to your project root and then run npm start.
  • Look in your package.json to see what else your start script is doing, and add the equivalent to your init.d script.
duncanhall
  • 11,035
  • 5
  • 54
  • 86
  • Sadly this didn't make any difference. I also tried running my local project in the same way, e.g. `/usr/local/bin/node /Users/Me/Documents/Projects/server.js` and that worked fine, logging as expected. There's no 'start' script in my package.json either - great suggestions but this doesn't look like this is what's wrong. – ConfusedNoob Feb 19 '16 at 01:48
  • I updated the /etc/init.d script to change working directory and npm start. I then used `sudo -s` to run the script manually, e.g. `/etc/init.d/myscript start`. This works great, logging is good. However, when the script executes on reboot... nothing :( – ConfusedNoob Feb 20 '16 at 21:34