User avatar
richiepp
Posts: 140
Joined: Wed Dec 19, 2012 4:56 pm

Struggling to get a node.js script running with forever at r

Thu Jun 09, 2016 5:37 pm

Hey All,
I'm trying to fire a node script with forever @reboot and I'm having a hard time. Here's what I've tried.

Note my script does in fact work and can be called with the following

Code: Select all

forever start /var/www/html/node_stuff/app.js
but when I put it in the crontab it doesn't seem to fire

Code: Select all

@reboot forever start /var/www/html/node_stuff/app.js
when I add a line to /etc/rc.local I do get forever to start but I don't see the output of my script

Code: Select all

sudo -u pi -i forever start /var/www/html/node_stuff/app.js
So I thought it might be a user issue and tried different users like; pi, root and www-data and they all run under forever but i don't see the output of the node script. I verified that forever was working with

Code: Select all

forever list
Since I can get it to work from the command line as the pi user that should work right? Below is the content of my script, but I don't think it's relevant to the issue at hand.

How can I get this node script functioning properly at reboot?
Thanks
Rich

Code: Select all

'use strict';
var serialport = require('serialport');
var SerialPort = serialport.SerialPort;
var parsers = serialport.parsers;

var port = new SerialPort('/dev/ttyUSB0', {
  baudrate: 115200,
  parser: parsers.readline('\r\n')
});

port.on('open', function() {
  console.log('Port open');
});

port.on('data', function(data) {
  console.log(data);
  fs.writeFile("test.txt", (data));
});

var fs = require('fs');
fs.writeFile("test.txt", "I need some temps!", function(err) {
    if(err) {
        return console.log(err);
    }

    console.log("The file was saved!");
});

Heater
Posts: 15990
Joined: Tue Jul 17, 2012 3:02 pm

Re: Struggling to get a node.js script running with forever

Wed Jun 15, 2016 3:54 am

This is what I do:

I have node.js program I want be run at boot time.

So I use the systemd init system of Raspian Jessie to to that.

I create a systemd service file like so:

Code: Select all

[Service]
WorkingDirectory=/home/pi/propanel
ExecStart=/usr/local/bin/node --expose-gc /home/pi/propanel/assets/server-bundle.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=propanel
User=root
Group=root
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target
And I put it in the directory /etc/systemd/system

Now I enable the thing to run at boot time:

$ systemctl enable propanel

And I might start it immediately:

$ systemctl start propanel

Of course you will have to change the "ExecStart" and other details of that example to fit your case.
Memory in C++ is a leaky abstraction .

Return to “Other programming languages”