1

I have a gps tracking script using node-gpsd module. When the automobile pass over a checkpoint the script plays a sound like "you are in XXXXX stop" and then waits 30 second to start tracking for the automobile ubication. All this tracking system is running on a raspberry pi zero in a bus.

Here is my code:

var turf = require('turf');
var fs = require('fs');
var gpsd = require('node-gpsd');
var listener = new gpsd.Listener();
var exec = require('child_process').exec;
var sleep = require('sleep');

function estaDentro(long,lat,coords){
    var posicion = turf.point([long, lat]);
    var pol = turf.polygon([coords]);
    return turf.inside(posicion, pol);
};

function operacion(tpv) {
        var lat = tpv.lat;
        var lng = tpv.lon;
        var zonasJson = fs.readFileSync("audio/jsons/zonas.json");
        var zonas = JSON.parse(zonasJson);

        for (i=0; i<zonas.zonas.length; i++){
            if(estaDentro(lng,lat,zonas.zonas[i].coords)===true){
                var zona = i;
                var paradaJson = fs.readFileSync("audio/jsons/"+zona+"paradas.json");
                var parada = JSON.parse(paradaJson);
                for(x=0; x<parada.paradas.length; x++){
                    if(estaDentro(lng,lat,parada.paradas[x].coords)===true){
                        exec('mpg123 audio/arribando.mp3 audio/paradas/'+parada.paradas[x].nombre+'.mp3')
                        listener.unwatch()
                        sleep.sleep(30)
                        setTimeout(function(){ listener.watch(); }, 30000);
                  } 
                }
            }
        }
};

function correr() {
    listener.on('TPV', function (tpv) {
        operacion(tpv);
    });

    listener.connect(function() {
        listener.watch();
    });
    }

correr();

I need to run this script at boot, but I cant, really, I tried in /etc/rc.local writing this on that file:

/usr/local/bin/node /home/pi/trak3.js &

Then I reboot and nothing happens, in the log it appears that the command its run but for some reason it closes.

Another approach was inserting in crontab like this

@reboot /usr/local/bin/node /home/pi/trak3.js &

This works, but the script runs with an error, it play the sound repeatedly 3 times in a row, but if I run the script myself writing in the command line it play the sound once.

I dont know why I cant run node via the /etc/rc.local and why that happen when it run via crontab. This is making me crazy haha.

Thank you

=============Update 05 November 2016 ==============

I got the conclusion that the problem is caused by the paths of the modules, because I made a test script without dependencies and it executed fine at boot, I can get the path from modules downloaded from npm, but I cant get the paths from the integrated modules.

    var turf = require('/home/pi/node_modules/turf');
    var fs = require('fs');
    var gpsd = require('/home/pi/node_modules/node-gpsd');
    var listener = new gpsd.Listener();
    var exec = require('child_process').exec;
    var sleep = require('/home/pi/node_modules/sleep');

As you can see I need the "fs" and "child_process" path modules, do anybody knows how to solve this problem? thank you.

  • Why are you using sudo here? Whoever you are copying does not know what they are doing. – goldilocks Nov 03 '16 at 08:20
  • "...but for some reason it closes" -> May be useful: http://raspberrypi.stackexchange.com/questions/40493/log-output-of-background-or-boot-script – goldilocks Nov 03 '16 at 18:00

3 Answers3

1

try using pm2. it's designed to run node.js scripts at boot, among other things. you can restart a script, you can look at its regular and error outputs in log files.

to start a node.js script at boot:

$ pm2 start app.js

for help:

$ pm2 -h

for a list of running processes:

pm2 list

to get details about one of the processes:

pm2 show
mahesh
  • 111
  • 1
1

Most of us write and test our code as user "pi". At startup the Pi is running as "root" with a completely different environment and directory. I found it helpful to use "sudo -u pi ..." in my startup scripts to switch to the pi user as needed. Here's an example: https://github.com/oyamist/oyapi/blob/master/scripts/oyapi

Note also the explicit cd into the desired directory. Yours will be different.

OyaMist
  • 1,119
  • 6
  • 8
0

I had the same infuriating problem then I remembered I previously had to export NODE_PATH in my .bashrc file to start node. Your rc.local file should contain the path to your node_modules directory. Eg:

export NODE_PATH=/usr/local/lib/node_modules

/usr/local/bin/node /home/pi/trak3.js &

Caleb Carroll
  • 91
  • 1
  • 1
  • Hi, I tried your solution but nothing happened.

    After some analysis I got the conclusion that the problem is with the modules path, I can get the path from modules downloaded from npm because the files are saved in /home/pi/node_modules. But this only applys to the modules downloaded from NPM the fs and child process modules are causing the problem.

    Do you know where are located "fs" and "child_process" modules?

    – Fernando Aguirre Nov 05 '16 at 08:07