I'm trying to run the code bellow from a PHP page on my beaglebone black:
import Adafruit_BBIO.PWM as PWM
red = "P8_13"
green = "P8_19"
blue = "P9_14"
PWM.start(red, 0)
PWM.start(blue, 0)
PWM.start(green, 0)
PWM.set_duty_cycle(red, 100)
PWM.set_duty_cycle(green, 0)
PWM.set_duty_cycle(blue, 0)
This code is just to turn my RGB LED to red.
My PHP page bellow:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>RGB LED</title>
</head>
<body>
Testing led.py execution...<br>
<?php
exec( "python led.py" );
?>
<br>End of execution!<br>
</body>
</html>
I'm using Apache2 and PHP5.
The code is on the same path of PHP page ("/var/www"). I already tried exec, shell_exec and system commands to execute the script. The LED doesn't turn to red. The code already have 777 permission. I really don't know why it isn't working. Does anyone have any idea?
Thank you.
I don't really want to go deep with this problem. But I am writing you this because I once played with a BBB and had a funny permission error while running python scripts. I was running commands without sudo. Please try it. I don't why it should have a sudo but it works!
Problem solved.
Thank you #furas.
I added "www-data ALL=(root) NOPASSWD: /usr/bin/python" to the sudoers file and also changed exec("python led.py") to exec ("sudo python led.py").
Related
I've made a simple webpage that has to run some simple bash scripts. I run apache2 on Jetson Nano and I have three files in /var/www/html folder:
index.html
testexec.php
test.sh
First one looks like:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>TEST</title>
</head>
<body>
<form action="testexec.php">
<input type="submit" value="Create file">
</form>
</body>
</html>
the php file:
<?php
$output = shell_exec("test.sh");
header('Location: http://192.168.25.16/index.html?success=true');
?>
script:
#!/bin/bash
touch file.txt
My problem is that everything looks good, but the scrit doesn't run. In future it will be used to run program written in python, but for now I can't run even that simple one. Is the problem with location of files or with something else?
I've already tried to change php file
$output = shell_exec("test.sh");
with
$output = exec("test.sh");
with or without $output
My browser (firefox) returns no errors in console.
Script works fine when I run it from shell. It is executable.
I've already tried to look for similar problems, but there were no solutions.
Very often the problem is with user rights, first try installing chmod 777 test.sh to just check if this is the problem.
In addition, you should go back to the exec documentation
exec(string $command, array &$output = null, int &$result_code = null): string|false
and check what $result_code is equal to.
If we get echo result!:
$output=null;
$retval=null;
exec('echo $(pwd);', $output, $retval);
echo "${retval} with value:\n";
print_r($output);
I want to call the python file from php using exec() but the output always is blank, and i do not know what is the problem, can anybody help me?
This is php code
<?php
exec("D:\python27\python.exe C:\xampp\htdocs\hi.py");
?>
This is python code
print "Content-Type: text/HTML"
print
print"""
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
"""
I can run the python file along and the browser will display hello world!, however when i using exec() in php, it would not work.
as you said you are running it like apache->php->shell_exec(SUDO..)
So the apache user has to be in sudoers file, better you don't give sudo to apache instead give apache (www-data) user right to run your python program
put first line in your python script: #!/usr/bin/env python so the script knows which program to open it with..
then
change group:
chgrp www-data /path/to/python-script.py
make it executabel
chmod +x /path/to/python-script.py
try it
shell_exec("/path/to/python-script.py");
I hope it works ;)
TIPP: Apache and PHP are for delivering Documents and Strings, if you want some kind of control and an API start with nodejs and https://www.npmjs.com/package/rpi-gpio package. This way you will have one place for your solid automation environment
My web host supports PHP. I have successfully run info.php and get pages of stuff to prove it in my browser.
When I try to execute a tiny piece of PHP in a tiny piece of HTML, as in the following hallowed script, I see nothing at all in my browser. Why? Is there something I am supposed to do?
Note: I use RapidWeaver as my principal site generation tool.
<html>
<head>
<title> PHP Test Script </title>
</head>
<body>
<?php
print "Hello World!";
?>
</body>
</html>
Make sure that your file extension is indeed .php, so that the web-server knows how to handle the file. Also, check your error-logs, there might be something in there if you get no output at all. You can do that in your terminal by running php --info | grep error. On your file-system, the error-log is stored in either /var/log/httpd/error_log or /var/log/apache2/error_log (if you're on Linux).
My problem is that I've got a python Script in the Webspace folder of my RaspPI my PHP Script should call but it doesn't.
I already set the python file permission to everyone.
But still i can't get this to work.
I tried several solutions but none of them helped me.
Here's what I did so far:
index.htm
<html>
<body>
<form action="photo.php" method="post">
<input type="submit" value="Take photo" name="test">
</form>
</body>
</html>
photo.php
if(isset($_POST['test']))
{
shell_exec('/var/www/photoburst.py');
echo'photo saved';
}
else
{
echo'Error.';
}
?>
I cant find any errors so please help me fix this.
Thanks!
Make sure your file has execution permission chmod a+x /var/www/photoburst.py
Instead you could call /usr/bin/python /var/www/photoburst.py and see if that works
What Linux are you running? Raspbian? N00bs?
For everyone having the same problem: This is the solution:
First you need to give full write/read and execute permission to the camera module AND the var/www folder.
This is done by typing:
sudo chmod 777 /var/www
sudo chmod 777 /dev/vchiq
into the Shell command line
maybe you need to get root rights by typing
sudo bash
before.
Next thing is to change the image save path folder to var/www because this now is fully accessible.
When done so, you need to modify photo.php like that
<?php
if(isset($_POST['test']))
{
$command = escapeshellcmd('/var/www/photoburst.py');
$output = shell_exec($command);
echo $output;
echo'Saved!';
}
else
{
echo'No Info submitted from the form.';
}
?>
Last thing is to tell the python file to, well be a python file with the shebang line now:
When done, photoburst.py should look similar to this one:
#!/usr/bin/python
import time
import picamera
import datetime
camera=picamera.PiCamera()
camera.capture('/var/www/'+datetime.datetime.now().strftime("IMG %T%B%d%Y")+'.jpg')
camera.close()
So if you followed these steps, you probably be able to take a photo with the raspberry cam through PHP.
A big thank you to all of the posters here.
Without your help I would never have found out my problem.
This is my php script
<HTML>
<head>
<h1>Hello</h1>
</head>
<body>
<?php
$command=escapeshellcmd('~/PycharmProjects/Test_1/wiki_update.py 2 2>&1');
$output=shell_exec($command);
echo $output;
?>
</body>
</HTML>
I get no output when I run it through the browser.
When I change the
$command=escapeshellcmd('whoami');
it outputs as NOBODY,
I granted permission to nobody for that python script still no output.
I am using xampp for linux and php script in htdocs/xampp.
Are you sure you don't need to change your command to run python?
$command=escapeshellcmd('python ~/PycharmProjects/Test_1/wiki_update.py 2 2>&1');
You also might try specifying absolute path - the ~ alias changes based on current user (which is the web server's user, not yours).