As the title says, when i try manually executing the script by double clicking and pressing execute through terminal. It works correctly
However when i run my php script or typing the php into the terminal window
/usr/bin/php start_cam.php
It locks up (the command doesn't finish with the "$", "$" does not show in terminal window to show it has ended the task and it doesn't work correctly
Below is my php script
<?php
$command =escapeshellcmd("/bin/bash cmd_start_cam.sh");
$output = shell_exec($command);
echo $output;
echo "php_startcam2";
?>
Below is my bash script(cmd_start_cam.sh)
!/bin/bash
echo 'running start camera script'
cd
sudo chmod 755 /etc/rc.local
cd
cd RPi_Cam_Web_Interface
sudo chmod u+x RPi_Cam_Web_Interface_Installer.sh
sudo ./RPi_Cam_Web_Interface_Installer.sh stop
sudo ./RPi_Cam_Web_Interface_Installer.sh start
echo 'complete start camera script'
Note: I use cd to ensure that im at my root directory as there where the files are. As its working via manual execution, do not think there a path issue?
Any help is greatly appreciated. thank you
Update: this is the error im experiencing output by the terminal command window:
i think i am experiencing a broken pipe as after my bash script command echo 'complete start camera script'
terminal window output cmd_start_cam.sh: line 12: echo: write error: Broken Pipe
and doesn't end with a $
like it should on normal execution
By the way this is running on raspberry pi 2
Update Solved/Solution:
Thanks to @ikra insight on checking the apache log file, which lead me to discover that the root cause was permission access. www-data needs to be added to the sudoers file.
- Instructions on backing up and editing sudoers file :http://raspbypi.com/enabling-the-sudo-command-for-a-new-user/
- sudo visudo
- add this at the end of the file www-data ALL=(ALL) NOPASSWD: ALL
- Press CTRL+X and press yes
- login and logout to ensure permission is now set.
- If your sudoer file gets corrupted: type this in terminal window to fix whatever text you have typed wrongly
pkexec visudo
.(Source: https://askubuntu.com/questions/73864/how-to-modify-a-invalid-etc-sudoers-file-it-throws-out-an-error-and-not-allowi)
Thanks to @ikra insight on checking the apache log file, which lead me to discover that the root cause was permission access. www-data needs to be added to the sudoers file.
- Instructions on backing up and editing sudoers file :http://raspbypi.com/enabling-the-sudo-command-for-a-new-user/
- sudo visudo
- add this at the end of the file www-data ALL=(ALL) NOPASSWD: ALL
- Press CTRL+X and press yes
- login and logout to ensure permission is now set.
- If your sudoer file gets corrupted: type this in terminal window to fix whatever text you have typed wrongly
pkexec visudo
.(Source: https://askubuntu.com/questions/73864/how-to-modify-a-invalid-etc-sudoers-file-it-throws-out-an-error-and-not-allowi)
I recently published a project that allows PHP to obtain and interact with a real Bash shell (as root if requested), it solves the limitations of exec() and shell_exec(). Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd('php start_cam.php');
//the return will be a string containing the return of the command
echo $return1;
You can maintain the bash script and simply trigger it like my example. However seeing the script is just a bunch of bash command, why not trigger them directly into the shell, that way you can handle the return and any exceptions.
In terms of security this is far better than running apache as root or the wide open sudo permissions you list in your solution update. But letting PHP anywhere near root is always tricky.
The project i built achieves a root bash shell in one of 2 ways:
1) You allow apache the right to sudo python.
OR
2) You pass root credentials to the object every time you need a shell with root setup.
Pick your poison. :) Read the documentation.
Try to use your script with php-cli and put this line at the beginning to see what is happening.
#!/usr/bin/php -ddisplay_errors=E_ALL
Do
chmod 700 myscript.php
and run it this way
$ ./myscript.php
If you want to run it under your apache server, just insert this function after the opening php tag:
error_reporting(E_ALL);
will do the same.
To pass your password to the sudo command, use sudo -S *command* < <(echo -e "*pass*\n")
in your script shell. However, that is a bad idea, because everyone who can access your script can get your password.
I guess your script starts your camera, if sudo ./RPi_Cam_Web_Interface_Installer.sh start
is a stream resource, then the issue is that shell_exec(./myscript.sh)
has a limited buffer value, thus it can't handle the response of your script shell.
Try to use popen()
instead of shell_exec()
I found an interesting link, which addresses an issue that is somewhat similar to yours https://stackoverflow.com/a/20109859/5280812