php shell_exec() 命令不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20124327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:54:15  来源:igfitidea点击:

php shell_exec() command is not working

phpshellpython-2.7

提问by user3018038

I am trying to run a .sh file from php. I tried doing it with shell_exec(). but its not working I refered many questions related to this in stack overflow but could not solve

我正在尝试从 php 运行一个 .sh 文件。我试着用 shell_exec() 来做。但它不起作用我在堆栈溢出中提到了许多与此相关的问题但无法解决

my php code is(web.php)

我的 php 代码是 (web.php)

    <?php
    echo shell_exec('/var/www/project/xxe.sh');
    echo "done";
    ?>

only done is printed. but it is working from terminal(php /var/www/project/web.php)

只打印完成。但它从终端工作(php /var/www/project/web.php)

In xxe.sh I am calling a python file

在 xxe.sh 中,我正在调用一个 python 文件

    python vin.py

I have also changed the file permission to 777 for both .sh n .py files please help

我还将 .sh n .py 文件的文件权限更改为 777 请帮忙

采纳答案by Jason Heo

If it works well in shell, I think apache is chrooted. So php can't find /var/...

如果它在 shell 中运行良好,我认为 apache 是 chroot 的。所以php找不到/var/...

Or user of httpd user does not have permission to enter /var/...

或者httpd用户的用户没有权限进入/var/...

If you are good at PHP. Open dir /var/... And readdir() and check dir exists and check file exists.

如果你擅长 PHP。打开 dir /var/... 和 readdir() 并检查目录是否存在并检查文件是否存在。

This question might help you. scanning /home/ with opendir()

这个问题可能对你有帮助。使用 opendir() 扫描 /home/

回答by SamV

If you say it works on the terminal and not on apache then apache's php.inifile may be disabling the use of shell_exec().

如果您说它适用于终端而不适用于 apache,那么 apache 的php.ini文件可能会禁用shell_exec().

See http://www.php.net/manual/en/ini.core.php#ini.disable-functions

http://www.php.net/manual/en/ini.core.php#ini.disable-functions

Your apache's php.inifile may look something like

您的 apachephp.ini文件可能类似于

disable_functions=exec,passthru,shell_exec,system,proc_open,popen

Remove shell_execfrom this list and restart the web server, although this is a security risk and I don't recommend it.

shell_exec从此列表中删除并重新启动 Web 服务器,尽管这是一个安全风险,我不建议这样做。

回答by Hyman Vial

While trying to run a script triggered by github post-receive webhook.

在尝试运行由 github post-receive webhook 触发的脚本时。

Here is where my project directory is located(cloned git repo):

这是我的项目目录所在的位置(克隆的 git repo):

/var/www/html/my-repo

I create a script inside the above directory called webhook.php:

我在上面的目录中创建了一个名为 webhook.php 的脚本:

<?php
#webhook.php

$cmd = shell_exec("git pull 2>&1");

#for debugging
echo $cmd;
?>

Execute the following command inside /var/www/html

在 /var/www/html 中执行以下命令

sudo chown www-data:www-data -R my-repo/

Test it by going to http://www.myserver.com/my-repo/webhook.php

通过访问http://www.myserver.com/my-repo/webhook.php对其进行测试

Add the path to your script to github webhooks.

将脚本的路径添加到 github webhooks。

回答by xchangcheng

I have been stuck in this problem for several hours.

我已经被这个问题困住了几个小时。

I have thought about a solution. 1. move your script to a python file "script.py" and place this file to your server root. 2. shell_exec("python script.py");

我想过一个解决办法。1. 将您的脚本移动到 python 文件“script.py”并将此文件放置到您的服务器根目录。2. shell_exec("python 脚本.py");

Any way, it works for me.

无论如何,它对我有用。

回答by Christopher Ditto

I tried everything here and nothing worked. What finally solved it for me was using the following before the shell_exec:

我在这里尝试了一切,但没有任何效果。最终为我解决的是在 shell_exec 之前使用以下内容:

putenv('PATH=/usr/local/bin');

回答by Jesper Blaase

The problem is usually that when you exec code from within php it is run as the webservers user www-data in alot of linux distros. Normaly this user does not have an enviroment set up, and because of that no PATH. By using full paths in your files you can usually overcome this.

问题通常是当您从 php 中执行代码时,它在许多 linux 发行版中以网络服务器用户 www-data 的身份运行。通常这个用户没有设置环境,因此没有路径。通过在文件中使用完整路径,您通常可以克服这个问题。

xxe.sh

xxe.sh

/usr/bin/python /path/to/script/vin.py

回答by lastlink

On my host I had to give a different path for my php file to be executed from shell_exec(). This didn't work shell_exec('/usr/bin/php backgroundtask.php');.

在我的主机上,我必须为从 shell_exec() 执行的 php 文件提供不同的路径。这没有用shell_exec('/usr/bin/php backgroundtask.php');

While this did shell_exec('/opt/php/php-5.5.0/bin/php backgroundtask.php');.

虽然这样做了shell_exec('/opt/php/php-5.5.0/bin/php backgroundtask.php');

You can visit this Reference.

您可以访问此参考。

回答by Nithiyakumar K

I had the same issue because PHP backslashes.

我有同样的问题,因为 PHP 反斜杠。

PHP escapes the backslashes, so the command that reaches the shell

PHP 转义反斜杠,因此到达 shell 的命令

'COPY E:path1\path2\file.prn /B 7.0.0.1\"PRINTER NAME"'

so I gave command like this

所以我发出了这样的命令

'COPY E:\path1\path2\file.prn /B \\127.0.0.1\"PRINTER NAME"'. 

You have to double-escape the backslashes: once for PHP and once for the shell.

您必须对反斜杠进行双重转义:一次用于 PHP,一次用于 shell。