php shell_exec() command is not working

2020-01-31 07:13发布

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

my php code is(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)

In xxe.sh I am calling a python file

    python vin.py

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

8条回答
爷、活的狠高调
2楼-- · 2020-01-31 07:21

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

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

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

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

查看更多
在下西门庆
3楼-- · 2020-01-31 07:21

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");

Any way, it works for me.

查看更多
The star\"
4楼-- · 2020-01-31 07:22

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

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

/var/www/html/my-repo

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

<?php
#webhook.php

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

#for debugging
echo $cmd;
?>

Execute the following command inside /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

Add the path to your script to github webhooks.

查看更多
老娘就宠你
5楼-- · 2020-01-31 07:24

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

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

Your apache's php.ini file may look something like

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

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

查看更多
beautiful°
6楼-- · 2020-01-31 07:36

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

putenv('PATH=/usr/local/bin');
查看更多
爱情/是我丢掉的垃圾
7楼-- · 2020-01-31 07:37

I had the same issue because PHP backslashes.

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

'COPY E:path1\path2\file.prn /B \127.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.

查看更多
登录 后发表回答