6

I have a simple PHP function that is supposed to execute a Pyton script when its called. I have tried this sort of function multiple times in my php programs, but somehow this time this function is not executing the python script at all. When I access the script from the command prompt and run python testing.py then it successfully gets executed. One thing that I want to mention that this script has some serious implementations of python's NLTK library, and takes more than 20 seconds to execute and perform its operations (i.e. data process and storing to db). Is this delay in the execution which is causing this problem or is there something else that I am missing this time?

function success(){
   $mystring = exec('python testing.py');
   $mystring;
   if(!$mystring){

        echo "python exec failed";
            }
   else{
   echo "<br />";
   echo "successfully executed!";
   }
alvas
  • 115,346
  • 109
  • 446
  • 738
khan
  • 7,005
  • 15
  • 48
  • 70
  • Did you enable error reporting? – Mike Mar 24 '13 at 03:46
  • yes, but nothing happened. It keeps echoing that python exec failed (as it is supposed to do). But once I go into command line and execute the script, it gets executed. – khan Mar 24 '13 at 03:48
  • It is very common that `exec` is disabled in the php config file. Try checking out this question first to see if that is the case: http://stackoverflow.com/questions/3938120/check-if-exec-is-disabled – Mike Mar 24 '13 at 03:50
  • 1
    You should update your question to the _actual_ problem, which you stated in a comment to @lenik's answer: _Its more about this script and the amount of time it takes to process i.e. more than 20 seconds. So, My question actually is that is there some feature in php that it waits for the script to be executed for a certain fixed period of time and then moves forward regardless of the script's execution success or failure??_ – Burhan Khalid Mar 24 '13 at 04:06
  • @Mike the exec() is enabled. – khan Mar 24 '13 at 04:09
  • @BurhanKhalid yes, this is part of the question but the main point is that script A is not getting executed while script B is getting executed by the same function. For executing script A, I have to go to command prompt and execute it from there.. – khan Mar 24 '13 at 04:19
  • How are you checking that the script isn't being executed? – Burhan Khalid Mar 24 '13 at 04:20
  • by looking into the database table where the script is supposed to populate the values. The values are only getting populated when I run the script manually from command prompt. :-( – khan Mar 24 '13 at 04:23

4 Answers4

15

you have to use full path for the python and for your file. you may find the former from the which python command, that most likely outputs '/usr/bin/python' and you should already know the latter. so your command would look like this:

$mystring = exec('/usr/bin/python /home/user/testing.py');

and you should make sure your python script has all appropriate permissions, because your web-server most probably is running as a different user, so permissions should be "-rwxrwxr-x" or something close.

lenik
  • 23,228
  • 4
  • 34
  • 43
  • the point i am making over here is that I tried running a simple script through the exact function success() and it worked. Its more about this script and the amount of time it takes to process i.e. more than 20 seconds. So, My question actually is that is there some feature in php that it waits for the script to be executed for a certain fixed period of time and then moves forward regardless of the script's execution success or failure?? – khan Mar 24 '13 at 04:03
  • In addition, make sure `exec` is allowed. – Burhan Khalid Mar 24 '13 at 04:04
  • 1
    PHP has some kind of limit, like 30sec, you may easily find from calling phpinfo() and searching for `max_execution_time` in the output. it's possible to change this from the script to a higher value, if you need, using `ini_set('max_execution_time', 1200);`, and you may get another details about `max_execution_time` from this question: http://stackoverflow.com/questions/4220413/how-does-php-max-execution-time-work – lenik Mar 24 '13 at 04:06
  • If I recall correctly (which I may not), when executing external commands or `sleep` it doesn't count that towards execution time except on Windows boxes. – Mike Mar 24 '13 at 04:13
  • I had to do: which python and then use: /usr/local/bin/python – talsibony Mar 13 '23 at 14:26
1

try to use exact path to the python program.

$mystring = exec('python testing.py');
Ryan Knopp
  • 582
  • 1
  • 4
  • 12
0

Try removing the $mystring; line

function success() {
   $mystring = exec('python testing.py');
   if(!$mystring){
       echo "python exec failed";
   } else {
       echo "<br />";
       echo "successfully executed!";
   }
}

For testing purposes try:

function success() {
   $mystring = exec('python testing.py', $output);
   var_dump($output);
}
doitlikejustin
  • 6,293
  • 2
  • 40
  • 68
  • 1
    Good eye, but I don't think it would actually affect it: http://codepad.viper-7.com/KT2UDx – Mike Mar 24 '13 at 03:54
  • 1
    @khan what is the script returning? You can store the output to check... exec('python testing.py', $output) - I updated the answer to show what I am talking about. – doitlikejustin Mar 24 '13 at 03:57
  • @doitlikejustin I think `var_dump` would be preferred in this case – Mike Mar 24 '13 at 03:59
  • I can make the script to return some thing, but the point is I have just executed a small python script with this and it got executed. It is only this script (which takes a huge amount of time to get executed) is actually not being executed from php exec. My suspicion is on the PHP that it is not executing the script for some memory size limitation or something else, because the script takes at least 20+ seconds to completely execute itself. – khan Mar 24 '13 at 04:08
  • Set a time limit of 60 seconds then... http://php.net/manual/en/function.set-time-limit.php – doitlikejustin Mar 24 '13 at 04:15
0

There is no issue with the exec() or anything.
The problem is that the nltk module is not able to locate the nltk_data directory. For it just locate where the nltk_data is present in your system: usually ~/nltk_data.
Now import add that path when you run the function.
import nltk;
Now, nltk.data.path is a list of locations where to search for the modules.
You can just do nltk.data.path.append("your location/directory");

Javesh
  • 1