This is how my python code looks like:
[root#localhost html]# vi brow1.php
<?php
echo exec("whoami");
echo exec("ls -l brow1.php") . "<br>";
$command = escapeshellcmd('/usr/temp_rohit.py');
shell_exec($command);
?>
I want to call python script from this php.
If i run the php script then it executes the python code but when run from the browser it does not execute the python script.
python script has all the required execute permission.
The output on the web browser is:
apache
-rwxrwxrwx. 1 root root 219 Nov 26 23:03 brow1.php
-rwxrwxrwx. 1 root root 115 Nov 26 22:38 /usr/temp_rohit.py
I am stuck at this point, Please help!
Related
I am running a PHP document on an Apache server on my Raspberry Pi and I want it to run a file when a button is clicked. I put some echo commands under the command to make the file run and it prints out but the file doesn't run. The index.php file and lightson.py and lightsoff.py files are all in the same directory (/var/www) and I have added #!/usr/bin/env python to the top of both files and made them executable by using chmod +x lightson.py. If I run the command from the shell it works and turns on the light just like I want with the exact same command as in the file but yet it wont run through the command. The code:
<html>
<head>
<title>Light Controller</title>
</head>
<?php
if (isset($_POST['LightON']))
{
shell_exec("sudo python /var/www/lightson.py");
echo("on");
}
if (isset($_POST['LightOFF']))
{
shell_exec("sudo python /var/www/lightsoff.py");
echo("Off");
}
?>
<form method="post">
<button name="LightON">Light ON</button>
<button name="LightOFF">Light OFF</button><br><br>
</form>
</html>
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
This worked for me:
test.php
<?php
echo shell_exec("python test.py");
?>
test.py
f = open("test.txt", "a+")
f.write("hiya buddy!!\n")
f.close()
print "some output"
Here's my relevant ls -l output from /var/www/html:
jason#Jason-one /var/www/html $ ls -l
-rw-r--r-- 1 jason jason 44 Sep 20 18:12 test.php
-rwxr-xr-x 1 jason jason 82 Sep 20 17:44 test.py
-rw-rw-rw- 1 jason jason 38 Sep 20 18:15 test.txt
Since I don't have GPIO pins on my laptop, I decided to write to a file as a test. Notice I didn't have to use sudo because of the way I set the permissions on test.py.
So this is my code for the raspberry pi to get a still shot from the raspicam and save it on a directory,
<?php
exec('raspistill -n -hf -o /var/www/img/image.jpg --timeout 1');
?>
I have given the ownership and the permission to read/write in that forlder using -R. so my ls -al in /var/www is this
drwxr-xr-x 3 www-data www-data 4096 Jun 19 08:05 .
drwxr-xr-x 12 root root 4096 Jun 19 05:54 ..
-rwxrwxrwx 1 www-data www-data 74 Jun 19 08:30 getImg
drwxrwxrwx 2 www-data www-data 4096 Jun 19 09:21 img
-rw-r--r-- 1 root root 70 Jun 19 10:07 index.php
getImg is the script i tried to run the script as a file like shell_exec('/bin/bash ./getImg'); that also doesn't work.
i have added /bash/bin and tried to run the script without using the script file too but that doesn't get the results.
How ever when i try to run the php file in the terminal, it creates the image as it normally should. So i figure this must be a permission issue, but what else should i do with the permissions? I have given all the rights to the directory.
EDIT
So I have found a workaround to this. since I don't know what the cause for the problem, i'd not mark this as an answer, but please vote it to appear at the top.
I now execute the script using the cgi scripts. I have created a shell script in the /usr/lib/cgi-bin/
#!/bin/bash
echo "Content-type:text/html\n"
sudo raspistill -vf -n -o /var/www/img/image.jpg --timeout 1200 --metering matrix
echo "Status: 204"
I saved this as capture and made this executable, did nothing with the permissions though.
sudo chmod +x capture
now when i open the link http://192.168.1.85/cgi-bin/capture the browser will still get me a 500 internal server error message. how ever, the image would still be created.
I would now need to get the 500 internal server error to be fixed.
[I'd add this as a comment but don't have enough points for it]
if you use the optional parameters $output and $return_var to capture the output and return value what do you get?
string exec ( string $command [, array &$output [, int &$return_var ]] )
does your command rely on environment variables that may be available when you run it as your user but not as www-data? (you can use the env command to check that)
does it still work if you run it via terminal after switching user to www-data?
I have web server that play music from Raspberry and turn on LED with script gpio.sh.
I am using mpd, mpc and gpio.
My /var/www/index.php :
<html><body><?php
echo exec('whoami');
if(isset($_POST['button1']))
{
shell_exec('mpc play')
shell_exec('/bin/bash /var/www/gpio.sh');
}
?>
<form method="post">
<p align=center>
OUTPUT (AUDIO) => <button name="button1">PLAY</button>
</p>
</form>
</body></html>
In terminal, I can run /bin/bash /var/www/gpio.sh successfully and LED turning on.
From web server 'mpc play' WORKS and can play a song BUT it can't run that gpio.sh .
The owner of index.php is www-data
-rwx------ 1 www-data www-data 1262 Dec 8 10:45 gpio.sh
-rwx------ 1 www-data www-data 272 Dec 9 09:39 index.php
What should I do ? When I change owner of index.php or gpio.sh to root, php can't execute.
Is my index.php wrong?
I can't execute .sh from php.
Please help.
Perfect solution for you would be to set SUID for script gpio.sh but unfortunately
you can't do so as far as gpio.sh is a script.
You have three options:
You can turn your script into the say C++ application and then set SUID
You can use some GPIO lib for python that don't force usage by root like: pigpio
You can set SUID for python interpreter, but I'm not sure its good idea generally
I have to execute a php script (a.php) in the background. I tried this but it's not working:
<?
$cmd = "php /home/megad404/www/prove/a.php &> /dev/null &";
exec('/bin/bash -c "'.$cmd.'"',$output,$return);
if($return===0)
{
echo 'Successful';
}
else
{
echo 'Unsuccessful';
}
?>
It returns "Successful" but it doesn't execute a.php
a.php:
<?
file_put_contents(date("s"),"");
sleep(5);
file_put_contents(date("s"),"");
sleep(5);
file_put_contents(date("s"),"");
?>
a.php writes a file every 5 second and it works fine, except if I try to execute it in the background with the first script.
You can try adapt mi script.
Look a command shell_exec() not exec(). First return all , second only last line.
function run_in_background($Command, $Priority = 0) {
if($Priority)
$PID = shell_exec("nohup nice -n $Priority $Command > /dev/null & echo $!");
else
$PID = shell_exec("nohup $Command > /dev/null & echo $!");
return($PID);
}
//Verifies if a process is running in linux
function is_process_running($PID) {
exec("ps $PID", $ProcessState);
return(count($ProcessState) >= 2);
}
and example
$PIDPHP=run_in_background("php -S 127.0.0.1:18086 ".__DIR__."/index.php"); // or any other process.
if (is_process_running($PIDPHP)){
exec("kill $PIDPHP");
}
You could also look into using real Posix/PCNTL functionality to actually detach the script to background, eg. with pcntl_exec and pcntl_fork(). This is after my opinion the right way to handle background scripts that runs for a longer period of time as you can communicate with the child/process to get updates, status and so on and even have them understand real signal handling.
PCNTL - http://www.php.net/manual/en/book.pcntl.php
POSIX - http://www.php.net/manual/en/book.posix.php
Cheers
This just worked for me:
<?php
$cmd = "/usr/bin/php /home/auser/a.php &> /dev/null &";
exec($cmd,$output,$return);
sleep(30);
if($return===0)
{
echo 'Successful';
}
else
{
echo 'Unsuccessful';
}
?>
I saved it as runa.php and ran it from the command window as php runa.php.
It produced 3 files.
running a.php also worked from the cron job:
]$ crontab -l
18 * * * * /usr/bin/php /home/auser/a.php
I put the script in a web directory and find that I have some writing problems. What can you see in the server log?
sudo tail -f /var/log/httpd/error_log
And what if you hit a.php from the web browser? Because you mention the script is 755, but how about the directory. Maybe it needs to be 775 or 777 for testing so that the script can write a file?
For testing I created a sub directory "output" and changed a.php
<?php
ini_set('date.timezone','America/New_York'); //without this it makes extra messages
error_log("a.php putting contents", 0);
file_put_contents("output/".date("s"),"");
sleep(5);
file_put_contents("output/".date("s"),"");
sleep(5);
file_put_contents("output/".date("s"),"");
error_log("a.php done", 0);
?>
It was unable to write files until I gave write permission to the ouput folder
sudo chmod 777 /var/www/html/output
Then I found out the apache user is writing the files:
~]$ sudo ls -l /var/www/html/output/
total 0
-rw-r--r--. 1 apache apache 0 Apr 18 11:38 00
-rw-r--r--. 1 apache apache 0 Apr 18 11:38 05
-rw-r--r--. 1 apache apache 0 Apr 18 11:37 55
So I changed the owner of output, in order to tone down the permissiosn again.
~]$ sudo ls -lu /var/www/html/ | grep output
drwxr-xr-x. 2 apache root 4096 Apr 18 12:21 output
This also works now:
~]$ sudo ls -l /var/www/html/output
total 0
-rw-r--r--. 1 apache apache 0 Apr 18 12:21 44
-rw-r--r--. 1 apache apache 0 Apr 18 12:21 49
-rw-r--r--. 1 apache apache 0 Apr 18 11:37 55
Encountering a problem when running phpagi:
-- Executing [123#DLPN_C:1] AGI("SIP/1000-00000001", "hello_world.php") in new stack
-- Launched AGI Script /var/lib/asterisk/agi-bin/hello_world.php
hello_world.php: Failed to execute '/var/lib/asterisk/agi-bin/hello_world.php': Exec format error
-- Auto fallthrough, channel 'SIP/1000-00000001' status is 'UNKNOWN' Scheduling destruction of SIP dialog '343930130' in 32000 ms (Method: INVITE)
From command line:
root#asterisk-test:/var/lib/asterisk/agi-bin# php5 -q hello_world.php
#!/usr/bin/php5 -q
Additional info:
-rwxr-xr-x 1 root root 757 Mar 29 19:32 hello_world.php
drwxrwxr-x 4 root root 4096 Mar 29 19:44 phpagi
-rwxr-xr-x 1 root root 25079 Sep 30 2010 phpagi-asmanager.php
-rwxr-xr-x 1 root root 2322 Sep 30 2010 phpagi-fastagi.php
-rwxr-xr-x 1 root root 67615 Sep 30 2010 phpagi.php
Source of hello world: http://www.eder.us/projects/phpagi/phpagi/api-docs/__examplesource/exsource_home_html_projects_phpagi_phpagi_examples_dtmf.php_acb7257145e4a5249182c8373cd8e848.html
The Exec Format Error is from /bin/bash, asterisk executes hello_world.php as a bash script.
shebang
If you add a correct shebang, the script get executed by the given PHP intepreter.
The first Line tells the System which program should run the script.
#!/usr/bin/env php
To test your shebang, execute the script itself, not by PHP:
root#asterisk-test:/var/lib/asterisk/agi-bin# ./hello_world.php
Make sure it is executable with:
root#asterisk-test:/var/lib/asterisk/agi-bin# chmod +x hello_world.php
alternative wrapper
Create a bash script that executes the PHP script.
example hello_world.sh:
/usr/bin/php hello_world.php
and call it in the Dialplan AGI("hello_world.sh").
Make sure the shellscript is executable chmod +x hello_world.sh.
I added following line on top script to get it working for me
#!/usr/bin/php -q
You issue is not asterisk issue,but general linux one.
Please try from your command line following:
su asterisk -c "/var/lib/asterisk/agi-bin/hello_world.php"
Most likly reasons: php path is incorrect or selinux enabled and not configured.
Could you check your extensions.conf or extensions_custom.conf, if the extension and priority are not continuous also this error will occur.
please check the below example:
[context]
exten => 1,1,Answer()
exten => 1,2,AGI(your-agi-script)
exten => 1,3,Hangup()