peekaboo peekaboo - 6 months ago 27
Bash Question

Executing bash script via PhP gives a different result compared to executing manually

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: http://askubuntu.com/questions/73864/how-to-modify-a-invalid-etc-sudoers-file-it-throws-out-an-error-and-not-allowi)


Answer

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: http://askubuntu.com/questions/73864/how-to-modify-a-invalid-etc-sudoers-file-it-throws-out-an-error-and-not-allowi)
Comments