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

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

问题描述

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

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

/usr/bin/php start_cam.php

它锁定(命令没有以$"结束,$"没有显示在终端窗口中以表明它已经结束了任务并且它不能正常工作

下面是我的php脚本

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

!/bin/bashecho '运行启动相机脚本'光盘须藤 chmod 755/etc/rc.local光盘cd RPi_Cam_Web_Interface须藤 chmod u+x RPi_Cam_Web_Interface_Installer.sh须藤 ./RPi_Cam_Web_Interface_Installer.sh 停止须藤 ./RPi_Cam_Web_Interface_Installer.sh 启动echo '完成启动相机脚本'

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

更新:这是我遇到终端命令窗口输出的错误:

我想在我的 bash 脚本命令 echo 'complete start camera script'

之后,我遇到了管道损坏

终端窗口输出 cmd_start_cam.sh: line 12: echo: write error: Broken Pipe 并没有像正常执行时那样以 $ 结尾>

顺便说一下,这是在 raspberry pi 2 上运行的

<块引用>

更新解决/解决方案:

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

  1. 关于备份和编辑 sudoers 文件的说明:http://raspbypi.com/enabling-the-sudo-command-for-a-new-user/
  2. sudo visudo
  3. 在文件末尾添加这个 www-data ALL=(ALL) NOPASSWD: ALL
  4. 按 CTRL+X 并按是
  5. 登录和注销以确保现在设置权限.
  6. 如果您的 sudoer 文件损坏:在终端窗口中输入此内容以修复您输入错误的任何文本 pkexec visudo.(来源: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 文件中.

  1. 关于备份和编辑 sudoers 文件的说明:http://raspbypi.com/enabling-the-sudo-command-for-a-new-user/
  2. sudo visudo
  3. 在文件末尾添加这个 www-data ALL=(ALL) NOPASSWD: ALL
  4. 按 CTRL+X 并按是
  5. 登录和注销以确保现在设置权限.
  6. 如果您的 sudoer 文件损坏:在终端窗口中输入此内容以修复您输入错误的任何文本 pkexec visudo.(来源:https://askubuntu.com/questions/73864/how-to-modify-a-invalid-etc-sudoers-file-it-throws-out-an-error-and-not-allowi)

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)

解决方案

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)

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

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