PHP 运行 bash (.sh) 脚本。 dpkg --info 有效,但 dpkg -i 无效
示例 PHP 脚本如下所示:
#!/usr/bin/php
$file = '/private/var/www/app/install.sh';
if(is_file($file)){
$output = shell_exec('bash /private/var/www/app/install.sh');
fwrite(STDOUT, $output."\n");
}
exit(0);
install.sh 示例
#!/bin/bash
clear
echo "Executing Install Script..."
dpkg --info /private/var/www/app/app.deb
dpkg -i /private/var/www/app/app.deb
echo "Script Finished"
exit 0
这将打印出生成的 dpkg --info 数据,但不会运行 dpkg -i。它不会打印出任何错误或任何内容...
此外,这是通过网络浏览器执行的。当脚本从终端运行时,它工作正常。但在网络上,仅返回 info 命令。
Sample PHP Script looks like this:
#!/usr/bin/php
$file = '/private/var/www/app/install.sh';
if(is_file($file)){
$output = shell_exec('bash /private/var/www/app/install.sh');
fwrite(STDOUT, $output."\n");
}
exit(0);
install.sh Example
#!/bin/bash
clear
echo "Executing Install Script..."
dpkg --info /private/var/www/app/app.deb
dpkg -i /private/var/www/app/app.deb
echo "Script Finished"
exit 0
This will print out the resulting dpkg --info data, but it will not run dpkg -i. It doesnt print out any errors, or anything at all...
Also, this is being executed via a web web browser. When the script is run from the terminal, it works fine. But on the web, only the info command is returned.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在这里尝试执行的操作可能存在一些安全问题,我不会对此发表评论。但是,我猜测 dpkg -i 需要以 root 身份运行,而您的网络服务器(执行 install.sh 脚本)没有以 root 身份运行。另一方面,
dpkg --info
命令不需要 root 权限即可运行,因此在通过网络服务器的用户执行时您将看到其输出。如果您确实需要以 root 身份运行此脚本,您可能需要查看特定的/etc/sudoers
配置。也许从这里开始:https://help.ubuntu.com/community/Sudoers看看这里:http://ubuntuforums.org/showthread.php?t=1132821
There are probably some security concerns with what you are trying to do here, and I'm not going to comment on those. However, I'd guess that
dpkg -i
needs to run as root, and your webserver (which executes the install.sh script) is not running as root. On the other hand, thedpkg --info
command doesn't need root privileges to run, and so you will see its output when executed via the webserver's user. If you really need to run this script as root, you might want to look at a specific/etc/sudoers
config. Perhaps start here: https://help.ubuntu.com/community/Sudoersand take a look here: http://ubuntuforums.org/showthread.php?t=1132821
dpkg --info
不需要 root 权限,而dpkg -i
则需要。由于您在浏览器上运行脚本,这意味着您的脚本在 php/apache 的用户上运行。我相信,在大多数安装中,该用户没有 root 权限。为了克服这个问题,您可以创建某种处理器,该处理器将执行以下步骤:
创建
队列
表:浏览器脚本:
处理器脚本(假设
queue_processor.php
):,设置 is_processed = 2 最后一步是,在 crontab 上设置
queue_processor.php
并拥有它的所有者作为根用户:dpkg --info
doesn't require root permission whiledpkg -i
does. Since you run your script on browser, this mean that your script run on php / apache's user. I believe, on most installation, that user doesn't have root permission.To overcome this, you could create some sort of processor that will have the following step:
create
queue
table:browser script:
processor script (let's say
queue_processor.php
):The final step is, to set
queue_processor.php
on crontab and have it owner as root: