WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

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

Offline joef

  • WikiUser
  • *
  • Posts: 4
Running python script via node on startup
« on: March 11, 2019, 07: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: newbielink:javascript:void(0); [nonactive]
sudo python flashLed.py
For example sake, I have a node server that calls the same command via node-cmd using:
Code: newbielink:javascript:void(0); [nonactive]
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: newbielink:javascript:void(0); [nonactive]
sudo node /home/tc/server.js
Strangely running the following in bootlocal.sh works:
Code: newbielink:javascript:void(0); [nonactive]
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!

Online Rich

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 6599
Re: Running python script via node on startup
« Reply #1 on: March 11, 2019, 07: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

  • WikiUser
  • *
  • Posts: 4
Re: Running python script via node on startup
« Reply #2 on: March 11, 2019, 07: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

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

Online Rich

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 6599
Re: Running python script via node on startup
« Reply #4 on: March 11, 2019, 10:30:21 AM »
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

  • WikiUser
  • *
  • Posts: 4
Re: Running python script via node on startup
« Reply #5 on: March 12, 2019, 02: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.

Online Rich

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 6599
Re: Running python script via node on startup
« Reply #6 on: March 12, 2019, 07: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

  • WikiUser
  • *
  • Posts: 4
Re: Running python script via node on startup
« Reply #7 on: March 12, 2019, 07: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!