与手动执行相比,通过PhP执行bash脚本可得到不同的结果 [英] Executing bash script via PhP gives a different result compared to executing manually

查看:103
本文介绍了与手动执行相比,通过PhP执行bash脚本可得到不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,当我尝试通过双击并通过终端按执行来手动执行脚本时.它可以正常工作

As the title says, when i try manually executing the script by double clicking and pressing execute through terminal. It works correctly

但是,当我运行我的php脚本或在终端窗口中输入php

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

下面是我的php脚本

<?php
        $command =escapeshellcmd("/bin/bash cmd_start_cam.sh");
        $output = shell_exec($command);
        echo $output;
        echo  "php_startcam2";

?>

下面是我的bash脚本(cmd_start_cam.sh)

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' 

注意:我使用cd来确保im在我的根目录中以及文件所在的位置.由于它是通过手动执行工作的,因此不认为存在路径问题吗? 任何帮助是极大的赞赏.谢谢

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:

我认为我在执行bash脚本命令echo 'complete start camera script'

i think i am experiencing a broken pipe as after my bash script command echo 'complete start camera script'

终端窗口输出cmd_start_cam.sh: line 12: echo: write error: Broken Pipe,并且没有以$结尾的正常执行情况

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

通过这种方式在树莓派2上运行

By the way this is running on raspberry pi 2

更新解决方案/解决方案:

感谢@ikra对检查apache日志文件的深入了解,这使我发现根本原因是权限访问.需要将www-data添加到sudoers文件中.

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. 有关备份和编辑sudoers文件的说明: http ://raspbypi.com/enabling-the-sudo-command-for-a-new-user/
  2. sudo visudo
  3. 在文件www-data ALL的末尾添加此内容ALL =(ALL)NOPASSWD:ALL
  4. 按Ctrl + X,然后按是
  5. 登录和注销以确保现在已设置权限.
  6. 如果您的sudoer文件损坏:请在终端窗口中键入此文件,以纠正您键入错误的任何文本pkexec visudo.(来源: https://askubuntu.com/questions/73864/how-to-modify-a-无效的其他sudoers文件将其抛出错误并且不允许使用)
  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)

推荐答案

感谢@ikra对检查apache日志文件的深入了解,这使我发现根本原因是权限访问.需要将www-data添加到sudoers文件中.

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. 有关备份和编辑sudoers文件的说明: http ://raspbypi.com/enabling-the-sudo-command-for-a-new-user/
  2. sudo visudo
  3. 在文件www-data ALL的末尾添加此内容ALL =(ALL)NOPASSWD:ALL
  4. 按Ctrl + X,然后按是
  5. 登录和注销以确保现在已设置权限.
  6. 如果您的sudoer文件损坏:请在终端窗口中键入此文件,以纠正您键入错误的任何文本pkexec visudo.(来源: https://askubuntu.com/questions/73864/how-to-modify-a-无效的其他sudoers文件将其抛出错误并且不允许使用)
  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)

这篇关于与手动执行相比,通过PhP执行bash脚本可得到不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆