0

I am working on a project that has a webserver(thats currently running from my laptop) and a RaspberryPi. I made a simple website with HTML, PHP and Javascript and i have a working python programm on my RaspberryPi. What i want to do is, to start the python programm using php currently i am using this command.

exec('sudo python /var/www/LED-Ring/python/examples/LED-Ring.py');

When i run the command directly in the terminal

sudo python /var/www/LED-Ring/python/examples/LED-Ring.py 

it works just fine.

I have already added the following

www-data ALL=NOPASSWD: ALL

into sudo visudo (i know that this is a security risk but i dont care right now i just want to get it to work)

what do i need on the Pi even though the webserver is running on the laptop?

the only thing the raspi needs to be able to do is to open the browser and go on the website.(which it already does) and then whenever the webserver send the command, the python script should start

the problem is probably related to permissions because i can run the python script with

"sudo python /var/www/LED-Ring/python/examples/LED-Ring.py" 

but i cant run it with

"sudo -u www-data python /var/www/LED-Ring/python/examples/LED-Ring.py"

when i use that command i get

Failed to create mailbox device : Operation not permitted

File "/var/www/LED-Ring/python/examples/LED-Ring.py", line 52, in module strip.begin

File "build/bdist.linux-armv7l/egg/neopixel.py", line 106 in begin

RuntimeError: ws2811:init failed with code -9 (failed to create mailbox device)

Community
  • 1
  • 1

1 Answers1

0

Don't use exec pleaze it's unsafe .Just use subprocess and the you can pass info between them wiht intermidiate file or via prints on shell

import subprocess
import time

list_subproc = []
list_to_pass = []
list_to_pass.append(myvar1)
list_to_pass.append(myvar2)
list_to_pass.append(myvar3)
list_to_pass.append(myvar4)

p = subprocess.Popen(['python','dialog29.py',list_to_pass],stdout=subprocess.PIPE,stderr=subprocess.STDOUT) # Call subprocess
list_subproc.append(p.pid)

time.sleep(0.2)
line = p.stdout.readline().lstrip().rstrip()
line = line.decode()
print(line)

For your othe problem because i cant recreate the problem probably you provide a wrong path.Provide a full path and check.

  • what if i do shell_exec(); instead of exec()? i dont need to pass any variables to the python script i just want it to make some outputs to the gpio pins . if i use the exact same command in the terminal that i used in the exec() it works so the path shouldnt be the problem should it? – Hannes Ensbacher Mar 15 '20 at 09:05
  • Sorry misread , you can write with python the data you want to a file(intermidiate file) and then try from there.Im not familiar with gpio .Hope it helps – Ευάγγελος Γρηγορόπουλος Mar 15 '20 at 09:17
  • maybe i dont really understand what u mean or the other way around but i just want to start the script using php and give no data to it and recieve no data from it i just want it to start, the python program itself is just an example program from github where some LED on a neopixel ring light up – Hannes Ensbacher Mar 15 '20 at 09:42
  • see https://stackoverflow.com/questions/19735250/running-a-python-script-from-php – Ευάγγελος Γρηγορόπουλος Mar 15 '20 at 11:03