Any script I try running at startup does not work

I am trying to use crontab as of now, and in crontab I am running the script:

@reboot usr/bin/node.js /usr/local/lib/node_modules/blynk-library/blynk-test.js &

It does not seem to work even though I can execute these paths in the terminal and everything works fine.

How can I verify if crontab is even working?

@BigP0P5 some scripts seem to be ok with crontab and some with /etc/rc.local

Doesn’t run for me in crontab but ok in rc.local before exit 0 as:

node /usr/local/lib/node_modules/blynk-library/blynk-test.js &

Still nothing…

I sudo nano the etc/rc.local file and input it like you previously mentioned and I’m still not connecting.

But I execute the above and everything works fine.:confused:

@Costas Here is a list of services I have running at startup. If that helps.

pi@MyPi:~ $ sudo service --status-all
[ - ] alsa-utils
[ - ] anacron
[ + ] atd
[ + ] avahi-daemon
[ + ] bluetooth
[ - ] bootlogs
[ - ] bootmisc.sh
[ - ] checkfs.sh
[ - ] checkroot-bootclean.sh
[ - ] checkroot.sh
[ + ] console-setup
[ + ] cron
[ + ] dbus
[ + ] dhcpcd
[ + ] dphys-swapfile
[ + ] exim4
[ + ] fake-hwclock
[ - ] hostname.sh
[ - ] hwclock.sh
[ + ] kbd
[ + ] keyboard-setup
[ - ] killprocs
[ + ] kmod
[ + ] lightdm
[ - ] motd
[ - ] mountall-bootclean.sh
[ - ] mountall.sh
[ - ] mountdevsubfs.sh
[ - ] mountkernfs.sh
[ - ] mountnfs-bootclean.sh
[ - ] mountnfs.sh
[ + ] networking
[ - ] nfs-common
[ + ] ntp
[ - ] plymouth
[ - ] plymouth-log
[ + ] procps
[ + ] raspi-config
[ + ] rc.local
[ - ] rmnologin
[ - ] rpcbind
[ - ] rsync
[ + ] rsyslog
[ - ] sendsigs
[ + ] ssh
[ - ] sudo
[ + ] triggerhappy
[ + ] udev
[ + ] udev-finish
[ - ] umountfs
[ - ] umountnfs.sh
[ - ] umountroot
[ + ] urandom
[ - ] x11-common

This guy explains three options. You probably want the second one (my Blynk works fine from init.d directory, even on OpenELEC).

1 Like

@BigP0P5 I spent ages last night trying to get the script to run on reboot with the Pi. It runs fine on the Omega 2+ just in rc.local. Sometimes it worked with the Pi but sometimes it didn’t.

As pointed out by @Lichtsignaal the definitive method normally recommended is with /etc/init.d but even that sometimes fails on the Pi for node. The service in /etc/init.d will be running but not node.

So it requires the npm package forever and forever-service.

sudo cp /path_to_blynk-test.js/blynk-test.js /home/pi/app.js
sudo npm install forever -g
sudo npm install forever-service -g
sudo forever-service install BlynkService

Check if everything is working with:

sudo service BlynkService start
sudo service BlynkService status
sudo service BlynkService stop
sudo service BlynkService restart

Then you can use the easy start on reboot method:

sudo nano /etc/rc.local
sudo service BlynkService start
sudo shutdown -r now

After reboot the following will confirm the script and service is running

ps aux | grep node
sudo service BlynkService status
service --status-all | grep BlynkService

You will not see the Console but a full log will go to /var/log/BlynkService.log including any error messages for incorrect path’s etc. If the console is important you can always tail the log with:

tail -f /var/log/BlynkService.log

Tested and working flawlessly on one of my Pi’s but be patient waiting for the service to start.

See https://causeyourestuck.io/2016/04/30/run-node-js-script-startup/ and http://www.slidequest.com/q/70ang for further details

1 Like

in my /var/log/BlynkService.log

Error: Cannot find module 'blynk-library'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/home/pi/app.js:1:78)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)

error: Forever detected script exited with code: 1
error: Script restart attempt #1
module.js:338
throw err;

Full path required in app.js, so if you are declaring a var Blynk it is:

var Blynk = require('/usr/local/lib/node_modules/blynk-library');

First off I would like to thank you for your services. Would you like a coffee :coffee: Sorry if I sound like a n00b but, where above, am I declaring a variable called Blynk???

Might not be var Blynk for you in the require, but the path in the require needs to be the full path to the blynk-library in the node_modules.

Paste a bit of the js file that you are trying to run.

So edited the path into the script like above to read:

var BlynkLib = require('/usr/local/lib/node_modules/blynk-library');

var blynk = new BlynkLib.Blynk('myauthtokenishere');
var v1 = new blynk.VirtualPin(1);
var v9 = new blynk.VirtualPin(9);

v1.on('write', function(param) {
  console.log('V1:', param);
});

v9.on('read', function() {
  v9.write(new Date().getSeconds());
});



v9.on('read', function() {
  v9.write(new Date().getSeconds());
});

Still getting same error in log file.

@BigP0P5 does it run without error from the command line?

I’m just running with TCP at present. The following file called bigpop.js runs fine from the command line with:

node bigpop.js

You would need to rename it to app.js for the forever-service

var BlynkLib = require('/usr/local/lib/node_modules/blynk-library');

var AUTH = 'xxxxxxxxxxxxxx';
 
var blynk = new BlynkLib.Blynk(AUTH, options = {
  connector : new BlynkLib.TcpClient()
});

var v1 = new blynk.VirtualPin(1);
var v9 = new blynk.VirtualPin(9);

v1.on('write', function(param) {
  console.log('V1:', param);
});

v9.on('read', function() {
  v9.write(new Date().getSeconds());
});

v9.on('read', function() {
  v9.write(new Date().getSeconds());
});

Having copied the above to app.js and after running:

sudo service BlynkService restart

it is running just fine on my Smartphone.

Try from command line first and let me know how that goes.

pi@MyPi:~ $ node /usr/local/lib/node_modules/blynk-library/app.js
OnOff mode
Connecting to TCP: blynk-cloud.com 8442
Connected
Authorized
^C     
pi@MyPi:~ $ node app.js
module.js:338
    throw err;
          ^
Error: Cannot find module 'blynk-library'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/home/pi/app.js:1:78)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)

Try the following command:

export NODE_PATH=/usr/local/lib/node_modules

then try the script again

node app.js

It will work after I enter that, but like in the previous thread it won’t work after a reboot.

It should do.

If you can’t get it to work as it is try this in rc.local before the exit 0

export NODE_PATH=/usr/local/lib/node_modules
sudo service BlynkService start

No go…I think I need to just nuke and boot another distro.
but thanks for all your help.

What distro are you currently running and do you boot to the GUI?
Precisely which Pi do you have?

Raspberry Pi 3 running raspbian all updated and upgraded booting to the gui. Thinking about using an older version.

I don’t use the GUI, do you actually use it?