1

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.

  1. Instructions on backing up and editing sudoers file :http://raspbypi.com/enabling-the-sudo-command-for-a-new-user/
  2. sudo visudo
  3. add this at the end of the file www-data ALL=(ALL) NOPASSWD: ALL
  4. Press CTRL+X and press yes
  5. login and logout to ensure permission is now set.
  6. 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)
Community
  • 1
  • 1
peekaboo
  • 105
  • 1
  • 15
  • Those `sudo` prompts may be asking for a password. PHP isn't sending any input, so it just sits there. – gen_Eric Sep 22 '15 at 18:21
  • @RocketHazmat hmm but when i manually type out those commands without a script but with sudo it works. Ok but say it requires password, how do i parse password via php? – peekaboo Sep 22 '15 at 18:23

4 Answers4

1

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.

  1. Instructions on backing up and editing sudoers file :http://raspbypi.com/enabling-the-sudo-command-for-a-new-user/
  2. sudo visudo
  3. add this at the end of the file www-data ALL=(ALL) NOPASSWD: ALL
  4. Press CTRL+X and press yes
  5. login and logout to ensure permission is now set.
  6. 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)
Community
  • 1
  • 1
peekaboo
  • 105
  • 1
  • 15
1

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.

MerlinTheMagic
  • 575
  • 5
  • 16
0

You should either check:

  • SUID your shell-scripts, which are run with sudo
  • and make sure everything is OK with current directory (and PATH) for your PHP script.
Evgeniy Chekan
  • 2,615
  • 1
  • 15
  • 23
  • i dont get the SUID part, what if i have set permision 777 to the file ? do i still need SUID? – peekaboo Sep 22 '15 at 18:32
  • @peekaboo This question https://superuser.com/questions/440363/can-i-make-a-script-always-execute-as-root will make it clear, I believe – Evgeniy Chekan Sep 22 '15 at 18:33
  • hmmm judging from his other solution i might as well just chown the file change owner from www-data:www-data to root:root – peekaboo Sep 22 '15 at 18:36
0

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

Community
  • 1
  • 1
ikra
  • 1
  • 3
  • where do i put this line? `#!/usr/bin/php -ddisplay_errors=E_ALL` ?terminal command window or my script or php file? – peekaboo Sep 23 '15 at 00:52
  • i think i am experiencing a broken pipe as after my bash script command `echo 'complete start camera script' ` terminal output `cmd_start_cam.sh: line 12: echo: write error: Broken Pipe` and doesn't end with a `$` like it should on normal execution – peekaboo Sep 23 '15 at 00:57
  • ths line should appear at the beginning of your script `#!/usr/bin/php -ddisplay_errors=E_ALL` – ikra Sep 23 '15 at 01:16
  • but my script is a bash script wouldn't that change the interpreter? – peekaboo Sep 23 '15 at 01:18
  • i meant add it in your php script. try to remove the line 12 in your script shell, since it's not critical – ikra Sep 23 '15 at 01:21
  • i fixed a little mistake i made in the **sudo** command. try this instead `sudo -S command < <(echo -e "passwd\n")` – ikra Sep 23 '15 at 01:44
  • manually executing using `./script_name.php` works but if i do `/usr/bin/php script_name.php` it would get stuck any ideas? even after using popen – peekaboo Sep 23 '15 at 09:34
  • What `sudo ./RPi_Cam_Web_Interface_Installer.sh start` did exactly? – ikra Sep 23 '15 at 21:58
  • sorry was busy the past few days but i have solve the issue, your tips helped me out will post the solution. – peekaboo Sep 27 '15 at 21:29