I'm trying to run a Node.js script that connects to a HID device (HTC Vive tracker) and sends feature reports to it, to run from boot.

I've tried using rc.local, a systemd file but none of it works if I power on the Raspberry Pi, however running it from rc.local works if I manually reboot the Raspberry Pi.

The script uses Node-hid to initialise the connection and is invoked by simply using node /path/to/script.

  • Could you edit your question to include details of the HID device and the script? At the moment you are asking us to guess too much about what you might be doing. – joan Jan 15 '18 at 9:54
  • Unless the script is massive you should include the source and details of how it is invoked. – joan Jan 15 '18 at 9:57
  • What OS version is you using? And to have long sentences decrease the readability! And have you looked at croon's reboot feature? – MatsK Jan 15 '18 at 11:34
  • I'm running the latest version of raspbian, I just tried to use the cron reboot feature but still nothing happened. – ChrisOH Jan 15 '18 at 11:38
  • How do you use @reboot on crontab ? – Francesco Gusmeroli Jan 15 '18 at 13:05

Basically, you have to call the correct path to find node, so correct your script on crontab like that:

@reboot /path/to/node /path/to/script

to find path/to/node/ simply run that command: which node and the result is what you are looking for.

in your specific case is: /home/pi/bin/node

  • This has the same issue as running it from rc.local in the way that the script runs both times but only works, and I mean the tracker only starts being picked up again if the pi is rebooted but not when the pi is booted, is there a way to delay the running of the script in cron? Because it could be that the script is running before the hid device is initialised – ChrisOH Jan 15 '18 at 13:41
  • So if the script start both times but in one of them it not work, probably there is some issue inside code and not in the reboot way you start it, as you said, maybe you access some resource not already loaded or something similar, so try to add some wait on top of your script, or try tu edit @reboot sleep 60 && /path/to/node /path/to/script – Francesco Gusmeroli Jan 15 '18 at 13:46
  • It's still not working, all the code does is connect to the raspberry pi by using the path to the right interface and send feature reports on a timed interval so that the vive tracker picks it up – ChrisOH Jan 15 '18 at 14:06
  • try to setup a script that check the path to the right interface at startup and print it into a file or something like that and try to delay the sleep time – Francesco Gusmeroli Jan 15 '18 at 14:14

Kind of a hacky way of doing this but while testing I found out it was that the raspberry pi was 'distracting' the tracker if booted but not rebooted so I added an if statement to rc.local which would reboot it once.

if  [ -e '/home/pi/bootorreboot.txt' ]
    rm -rf /home/pi/bootorreboot.txt
    sleep 10 && reboot
    echo 'exists' > /home/pi/bootorreboot.txt
    node /home/pi/node_modules/node-hid/src/simple-tracker.js &

Not the answer you're looking for? Browse other questions tagged or ask your own question.