[英]Issues With PHP exec & shell_exec - Shell Script Execution
I've been unable to run php scripts that I need to use to start and stop webcam services that run on the local machine with the scripts. 我一直无法运行用于启动和停止使用脚本在本地计算机上运行的网络摄像头服务所需的php脚本。 I can find nothing in the logs to indicate why the script doesn't' work.
我在日志中找不到任何内容来指示为什么脚本不起作用。
I confess to being severely handicapped regarding PHP, especially server-side scripting. 我承认在PHP方面尤其是服务器端脚本编写方面存在严重的障碍。
The environment is Debian Jesse running Nginx with all required SSH and PHP modules installed 环境是运行Nginx且安装了所有必需的SSH和PHP模块的Debian Jesse
I have added www-data to the sudoers file with: 我已经将www-data添加到sudoers文件中:
www-data ALL=(ALL) NOPASSWD: /var/www/html/start_webcam.sh
Enabled the $PATH environment for www-data at: 在以下位置为www-data启用了$ PATH环境:
/etc/php5/fpm/pool.d/www.conf
The shell script resides in the .../html directory and runs from the terminal with no issues. Shell脚本位于... / html目录中,并且可以从终端运行而没有任何问题。
This is the code for both the php and shell scripts: 这是php和shell脚本的代码:
start_webcam.php: start_webcam.php:
<?php
echo exec('sudo bash /var/www/html/aspirebox/start_webcam.sh 2>&1, $output');
print_r($output);
?>
The $output and print_r stuff is there because it was the last thing I tried based on a post I found out here somewhere. $ output和print_r之所以存在,是因为这是我根据我在某处发现的帖子尝试的最后一件事。
start_webcam.sh start_webcam.sh
#!/bin/bash
service motion start
Thanks in advance to anyone out here that has a clue. 在此先感谢任何有线索的人。 After 2 days of wrestling with this, I am sure that I do not.
经过2天的搏斗,我确定自己不会。
according to Passing Variables to shell_exec()? 根据将变量传递给shell_exec()? you should change your code like this:
您应该这样更改代码:
<?php
$output = exec('/var/www/html/aspirebox/start_webcam.sh 2>&1 ');
print_r($output);
?>
and let your bash script execute as all (no need to sudo bash): 并让您的bash脚本全部执行(无需sudo bash):
chmod a+x /var/www/html/aspirebox/start_webcam.sh
Thank you very much - that worked. 非常感谢您-确实有效。
I worked through getting the path straight for the directory the shell script runs in, and the correct path to run "service". 我通过获取运行Shell脚本的目录的正确路径以及运行“ service”的正确路径来进行工作。
All I have now is to figure out why I'm getting "Failed to start motion.service: Access denied" 现在,我只想弄清楚为什么我会收到“无法启动motion.service:访问被拒绝”的信息
I've given www-data permission to run the script without a password on sudoers, have to keep digging. 我已获得www-data许可,可以在sudoers上运行无密码的脚本,必须不断进行挖掘。
Thanks again! 再次感谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.