26

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

user3018038
  • 263
  • 1
  • 3
  • 5
  • How are you determining it's "not working"? What side-effects does vin.py have that you're expecting but not seeing? – Wooble Nov 21 '13 at 15:00
  • 2
    As per the [docs](http://php.net/shell_exec), "It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required" – Marc B Nov 21 '13 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 – user3018038 Nov 21 '13 at 15:05

8 Answers8

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.

SamV
  • 7,548
  • 4
  • 39
  • 50
  • 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. – SamV Nov 21 '13 at 15:52
  • sorry I did not get. what shud i do? – user3018038 Nov 21 '13 at 15:59
  • Are you running php in safe mode? Also read through the comments here http://us2.php.net/shell_exec as some of them seem helpful. – SamV Nov 21 '13 at 16:11
  • Most web hosting services have disable by default this option and they do not let you enable it.. – Kwnstantinos Nikoloutsos Mar 02 '20 at 09:16
7

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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
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()

Community
  • 1
  • 1
Jason Heo
  • 9,956
  • 2
  • 36
  • 64
  • $ 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 – user3018038 Nov 21 '13 at 15:26
  • 2
    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 – Jason Heo Nov 21 '13 at 15:31
  • Works:" . $f . ""; } ?> is this the code u are talking about? – user3018038 Nov 21 '13 at 15:41
  • 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? – Jason Heo Nov 21 '13 at 15:52
  • If so, i think apache can't access the directory. The reason i dont know but this link can help you. http://stackoverflow.com/questions/7188806/scanning-home-with-opendir – Jason Heo Nov 21 '13 at 16:11
  • For securty reason there is restriction. Find where restriction came from. Several possiblity. – Jason Heo Nov 21 '13 at 16:12
  • in a.txt sh: 1: /var/www/project/xxe.sh: not found – user3018038 Nov 21 '13 at 16:28
  • system("/var/www/project/xxe3.sh > /tmp/a.txt 2>&1"); this worked! :) thank you soooo much :) – user3018038 Nov 21 '13 at 16:38
  • @user3018038 you had not changed anything but system() worked? shell_exec() still not working? it's very strange! anyway, good to see your reply. – Jason Heo Nov 21 '13 at 23:58
  • @user3018038 and I made some mistakes. there are simple way to check file or dir exists and they are executable. `file_exists()` and `is_executable()` respectively. I will modify my answer this weekend. – Jason Heo Nov 22 '13 at 00:52
  • The code in xxe.sh was to save a web page with wget. By executing the above command I got to know where the error was.(permission was denied to save). So I changed the permission of /var/www/. I know its not safe but Now even shell_exec is working. – user3018038 Nov 22 '13 at 07:25
4

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
Jesper Blaase
  • 2,320
  • 16
  • 13
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.

Jack Vial
  • 2,354
  • 1
  • 28
  • 30
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.

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.

Jon Miles
  • 9,605
  • 11
  • 46
  • 66
lastlink
  • 1,505
  • 2
  • 19
  • 29
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.

Nithiyakumar K
  • 22
  • 3
  • 12