WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Running python script via node on startup  (Read 3091 times)

Offline joef

  • Newbie
  • *
  • Posts: 4
Running python script via node on startup
« on: March 11, 2019, 10:08:08 AM »
Hey guys!

I have a python script that flashes an LED via the GPIO pins on the Pi.

This works perfectly fine when ran directly using:
Code: [Select]
sudo python flashLed.py
For example sake, I have a node server that calls the same command via node-cmd using:
Code: [Select]
cmd.run("sudo python flashLed.py")
When I run that node server manually, the led flashes away perfectly. However if I start that node server from bootlocal.sh I get no flashing led (the server is running correctly as i can hit it remotely).

bootlocal.sh has the following and doesn't led doesnt flash:
Code: [Select]
sudo node /home/tc/server.js
Strangely running the following in bootlocal.sh works:
Code: [Select]
sudo python flashLed.py
So I think it's something to do with node being started from bootlocal.sh not having root when it runs the python command, even though I'm running it with sudo.

Any ideas?

Thanks!

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11657
Re: Running python script via node on startup
« Reply #1 on: March 11, 2019, 10:18:12 AM »
Hi joef
... So I think it's something to do with node being started from bootlocal.sh not having root when it runs the python command, even though I'm running it with sudo. ...
Using  sudo  is not required for  bootlocal.sh  because it already runs as root.

Offline joef

  • Newbie
  • *
  • Posts: 4
Re: Running python script via node on startup
« Reply #2 on: March 11, 2019, 10:25:35 AM »
Thanks Rich!

Possibly something to do with it running in the background and not having access to GPIO perhaps?

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1249
Re: Running python script via node on startup
« Reply #3 on: March 11, 2019, 11:36:26 AM »
Does node require network?  The network is typically not fully ready when bootlocal.sh runs.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11657
Re: Running python script via node on startup
« Reply #4 on: March 11, 2019, 01:30:21 PM »
Hi joef
Paul_123 makes a very good point. Easy enough to confirm. Create a file called  flasher.sh  in your home directory containing:
Code: [Select]
#!/bin/sh
# Wait for network to come up
SEC=60
while [ $SEC -gt 0 ]
do
        $(( SEC-- ))
        ifconfig | grep -q "Bcast:" && break
        sleep 1
done
node /home/tc/server.js
Make the file executable. In your  bootlocal.sh  file replace the  sudo node /home/tc/server.js  line with:
Code: [Select]
/home/tc/flasher.sh &

Offline joef

  • Newbie
  • *
  • Posts: 4
Re: Running python script via node on startup
« Reply #5 on: March 12, 2019, 05:24:48 AM »
Thanks! I tried this, didn't make any difference I'm afraid.

Node is definitely starting up fine as I can hit the server remotely, when I hit the server it should flash the led, I get a response from node no problem, but no led flash!

If I start the server manually and not from bootlocal.sh, it all works fine.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11657
Re: Running python script via node on startup
« Reply #6 on: March 12, 2019, 10:09:37 AM »
Hi joef
Is running  node /home/tc/server.js  supposed to flash the LED? Or do you need to also run  cmd.run("sudo python flashLed.py")?
If that's the case, then it should have been:
Code: [Select]
#!/bin/sh
# Wait for network to come up
SEC=60
while [ $SEC -gt 0 ]
do
        $(( SEC-- ))
        ifconfig | grep -q "Bcast:" && break
        sleep 1
done
node /home/tc/server.js
cmd.run("sudo python flashLed.py")

Offline joef

  • Newbie
  • *
  • Posts: 4
Re: Running python script via node on startup
« Reply #7 on: March 12, 2019, 10:51:04 AM »
Hey Rich,

node /home/tc/server.js has logic that runs cmd.run("sudo python flashLed.py") when certain endpoints on the server have been hit. E.g every time a certain url is loaded, the led should flash, so it needs to be part of the node app and not separated out.

Thanks!