I have a PHP webpage on my raspberry pi with 2 buttons (on and off)
The on button button redirects to On.php
The off button redirects to Off.php
In "/usr/lib/cgi-bin" I have a python script that I would like to execute (script.py)
I can perfectly execute it from the terminal by typing
cd /usr/lib/cgi-bin
sudo python script.py
It works if I do it from the terminal.
The problem is the PHP file (On.php) in my "/var/www" folder.
This is what I wrote:
<?php
exec('cd /usr/lib/cgi-bin');
exec('sudo python script.py');
?>
Why is the script executing from the terminal, but not from my PHP?
You can't use sudo from a PHP script. Apache is running from an user (www-data generaly), so edit this file : /etc/sudoers
Then add this line :
www-data ALL=(ALL) NOPASSWD:ALL
Care ! this will authorize all functions to be called by a PHP script, you can adapt changing "ALL" by your script or Python command.
Then precise your user in your exec command :
<?php
exec('sudo -u www-data python /usr/lib/cgi-bin/script.py')
Try this out, it should be working:
<?php
system("cd /usr/lib/cgi-bin");
system("sudo python script.py");
?>
Or even this:
<?php
system("cd /usr/lib/cgi-bin && sudo python script.py");
?>
On an older Raspbian distribution you need to place your file in /var/www/file.py. So in your file.php you add:
{
exec("sudo python /var/www/file.py");
}
On a newer Raspbian Jessie you need to place your file in /var/www/html/file.py, so in your file.php you need to add:
{
exec("sudo python /var/www/html/file.py");
}
Or just any file.py
<?php
{
exec("sudo python test.py");
}
?>
Note: For this to work you need to edit a file first to add these lines to allow passwordless sudo
sudo nano /etc/sudoers
then go to the bottom and add this
pi ALL=(ALL) NOPASSWD: ALL<br>
www-data ALL=(ALL) NOPASSWD: ALL
Related
I've a shell_test.php file in /var/www/html folder with this code:
<?php
shell_exec('/var/www/html/config.sh');
?>
config.sh in the same folder has this code:
#!/bin/sh
sudo -u root kill -SIGHUP $(cat /var/www/html/mosquitto/mosquitto.pid)
When I run ./config.sh from folder, it runs.
When I run command in config.sh file directly in terminal, it
works too.
I've added this into sudoers file so that there is no need of password:
www-data ALL=(ALL) NOPASSWD: /var/www/html/config.sh
The thing is it's working fine when run using terminal in both the mentioned ways. Why is not executing when run in PHP?
Your problem is probably, that it is apache, www-data or some other user that is running your script and you try to run it as root.
Try without sudo -u root and change the group of the file to www-data with:
chown root:www-data your-script
As you say "It isn't outputting anything but my mosquitto broker is resetting every time it runs which lets me know"
I think you should replace
shell_exec('/var/www/html/config.sh');
with
$output = shell_exec('/var/www/html/config.sh');
echo $output;
According to php docs "shell_exec — Execute command via shell and return the complete output as a string"
shell_exec doesn't print by default; you have to store the string output and then use it
I made few changes in codes and it worked.
In shell_test.php, I changed code like this:
<?php
shell_exec('sudo -S ./config.sh');
?>
In config.sh, I changed like this:
#!/bin/sh
sudo kill -SIGHUP $(cat /var/www/html/mosquitto/mosquitto.pid)
i run script python from php in my raspberry pi and i wanna stop the program.
when i run program with php is running perfectly but when i run another file php to stop the program i failed.
this my code to run file python using php
<?php
shell_exec("/usr/bin/python /home/pi/Documents/python_project/multiple.py");
this my code to stop program
<?php
shell_exec("usr/bin/sudo killall python");
echo "kill executed"
thanks
A few problems were identified
1) www-data may not be in your sudoers file, check it is present, if not, add the line
www-data ALL=(ALL) NOPASSWD: ALL
to the file.
2) You are missing a "/" from the line "usr/bin/sudo", you should add it to be "/usr/bin/sudo" so the path is correct. usr/bin/sudo does not exist.
I have an sh file with file-removing commands.
I run it from php like this:
shell_exec("sudo -n ./truncatefiles.sh 2>&1");
Thats works fine if I open the PHP file from browser, but doesnt work from scheduled cron tab.
PHP user: www-data
If i run whoiami from cron, returns same: www-data
I added this to my visudo:
www-data ALL=(ALL) NOPASSWD: /www/sites/..../importscript/truncatefiles.sh
Shell exec for this sh file returns (from cron):
sudo: sorry, a password is required to run sudo
Why works it dirrefent way in cron?
What should I do for get it work?
PLease try to do the following,
Try to log your output from crotab to a file,
* * myscript.php >> /var/log/myjob.log 2>&1
This way you can debug your script.
1. Also the check the user and permissions for your shell script, php file.
2. try with sudo crotab -e
how to run bash script from php with sudo
index.php :
<?php
$arg;
exec("sudo /var/www/script", $arg);
?>
script :
!#/bin/bash
echo YOUR ARG IS $1;
sudoers www-data ALL=NOPASSWD /var/www/my
chmod 755 /var/www/my
Firstly, you can't sudo it directly like that. So you have to change some configurations in sudoers file.
Run sudo visudo in console, add the following line at the end of file
nobody ALL = NOPASSWD: /var/www/script
P.S.: It's a security risk to use your script like that which would sometime perform breach in data security in a great extent so my suggestion is don't call your script like that.
I'm trying to make a open source kiosk like system. When the web browser starts all programs, it will run in the browser using PHP. I've found this link: Program execution Functions. It's using:
<?php
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("WINWORD.exe", 7, false);
?>
I was able to start Microsoft Word on Windows, but I need to be able to do this on Linux.
This is what I've tried on my Linux server:
<?php
exec("/var/www/test.sh");
?>
But nothing happens. I know that test.sh works because I ran if from the terminal. I use test.sh to start a Python script. The Python script starts a text editor. I've tested the Python script and it works. All I need know is how to start the script from PHP on Linux.
Try out shell_exec.
shell_exec("/var/www/test.sh");
Also, make sure that the executable "permission" is set.
Additionally, you have to run the text editor as the logged-in user (propably you will do this with sudo or so) and to set the DISPLAY environment variable to :0.0 (or whatever is right for you). Example:
add this to /etc/sudoers:
ALL<tab>ALL=(kioskuser) NOPASSWD: ALL
(<tab> means that a real tab belongs there, edit the file by executing visudo as root)
content of the script:
#!/bin/bash
export DISPLAY=:0.0
sudo -u kioskuser /path/to/the/editor/command
I had to change the test.sh owner permission to www-data
then add
Cmnd_Alias RUN = /var/www/test.sh
www-data ALL = (root) NOPASSWD: RUN
To /etc/sudoers then the php code
<?php
shell_exec("sudo /var/www/test.sh");
?>