28

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

3
  • How are you determining it's "not working"? What side-effects does vin.py have that you're expecting but not seeing? Commented Nov 21, 2013 at 15:00
  • 2
    As per the docs, "It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required" Commented Nov 21, 2013 at 15:00
  • In python file I am trying to store some values into database. when I run php file on browser it is not storing any values Commented Nov 21, 2013 at 15:05

8 Answers 8

11

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Might want to check out the directory restrictions also, if the server is not live and is just for dev then try opening up the php.ini temporarily and see if that makes a difference.
sorry I did not get. what shud i do?
Are you running php in safe mode? Also read through the comments here us2.php.net/shell_exec as some of them seem helpful.
Most web hosting services have disable by default this option and they do not let you enable it..
8

shell_exec might not know what directory to look in for your executable's location directory. What solved it for me was this before the shell_exec:

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

Then the terminal can find the executable. Also check permissions on every part of the command to make sure apache user has read and execute permissions.

Comments

5

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()

11 Comments

$ ls -ld /var/ drwxr-xr-x 14 root root 4096 Dec 31 21:20 /var/ $ ls -ld /var/www drwxrwxrwx 6 root root 4096 Nov 18 02:00 /var/www
Not from shell, do directory listing in php using diropen() and readdir() and open php via web. This is a way checking apache can view /var/... If you don't how to directory list, google
<?php $d = opendir("/var/www"); $f = readdir($d); if(is_dir($f)) { echo "<b>Works:" . $f . "</b>"; } ?> is this the code u are talking about?
Yeah. It works? Then can you try this? system("/var/www/project/xxe.sh > /tmp/a.txt 2>&1") is there anything in a.txt?
If so, i think apache can't access the directory. The reason i dont know but this link can help you. stackoverflow.com/questions/7188806/scanning-home-with-opendir
|
5

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.

xxe.sh

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

Comments

3

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.

Comments

2

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.

Comments

1

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');.

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

You can visit this Reference.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.