3

I have a PHP website and I would like to execute a very long Python script in background (300 MB memory and 100 seconds). The process communication is done via database: when the Python script finishes its job, it updates a field in database and then the website renders some graphics, based on the results of the Python script.

I can execute "manually" the Python script from bash (any current directory) and it works. I would like to integrate it in PHP and I tried the function shell_exec:

shell_exec("python /full/path/to/my/script") but it's not working (I don't see any output)

Do you have any ideas or suggestions? It worths to mention that the python script is a wrapper over other polyglot tools (Java mixed with C++).

Thanks!

Ben
  • 54,723
  • 49
  • 178
  • 224
Laurențiu Dascălu
  • 2,039
  • 3
  • 22
  • 23
  • please include more details like the script is to run as web process, or via command line interface - in nutshell, 100seconds and the 300Mb probably will exceed allowed page execution time and memory - read this http://stackoverflow.com/questions/45953/php-execute-a-background-process – ajreal Nov 18 '10 at 11:04
  • - he said he is using shell_exec - so i assume he he is using his script as a commandline tool – Thariama Nov 18 '10 at 11:07
  • the script is a commandline tool – Laurențiu Dascălu Nov 18 '10 at 13:15
  • Here's a tip that's not directly related to your question, but may be useful: if this script takes a long time to run, you can do this to make it run in the background: `shell_exec("python /full/path/to/my/script > dev/null &")` – Jason Swett Nov 18 '10 at 14:51

6 Answers6

4

shell_exec returns a string, if you run it alone it won't produce any output, so you can write:

$output = shell_exec(...);
print $output;
John Conde
  • 217,595
  • 99
  • 455
  • 496
Brian
  • 41
  • 2
2

First off set_time_limit(0); will make your script run for ever so timeout shouldn't be an issue. Second any *exec call in PHP does NOT use the PATH by default (might depend on configuration), so your script will exit without giving any info on the problem, and it quite often ends up being that it can't find the program, in this case python. So change it to:

shell_exec("/full/path/to/python /full/path/to/my/script");

If your python script is running on it's own without problems, then it's very likely this is the problem. As for the memory, I'm pretty sure PHP won't use the same memory python is using. So if it's using 300MB PHP should stay at default (say 1MB) and just wait for the end of shell_exec.

Viper_Sb
  • 1,809
  • 14
  • 18
0

I found that the issue when I tried this was the simple fact that I did not compile the source on the server I was running it on. By compiling on your local machine and then uploading to your server, it will be corrupted in some way. shell_exec() should work by compiling the source you are trying to run on the same server your are running the script.

Nochbag
  • 77
  • 2
  • 6
0

A proplem could be that your script takes longer than the server waiting time definied for a request (can be set in the php.ini or httpd.conf).

Another issue could be that the servers account does not have the right to execute or access code or files needed for your script to run.

Thariama
  • 50,002
  • 13
  • 138
  • 166
  • You are right about the first problem, but how can I launch a background job from PHP? (also, how can I debug the issue that it works from command line and not from exec_shell) – Laurențiu Dascălu Nov 18 '10 at 13:16
  • well, you have a script - when executing this script from php via exec you need to get some kind of debug information. Why don't you just put some debugstatements into your script (write to STDERR or a file?) – Thariama Nov 18 '10 at 14:06
  • the problem is that the debug info is vague; I'm executing "black box" programs and they don't report too much. I changed the implementation and everything is fine now (check my answer). – Laurențiu Dascălu Nov 20 '10 at 09:51
0

Found this before and helped me solve my background execution problem:

function background_exec($command)
{
    if(substr(php_uname(), 0, 7) == 'Windows')
    {
        pclose(popen('start "background_exec" ' . $command, 'r'));
    }
    else
    {
        exec($command . ' > /dev/null &');
    }
}

Source:

http://www.warpturn.com/execute-a-background-process-on-windows-and-linux-with-php/

Alex Bailey
  • 1,679
  • 10
  • 15
0

Thanks for your answers, but none of them worked :(. I decided to implement in a dirty way, using busy waiting, instead of triggering an event when a record is inserted.

I wrote a backup process that runs forever and at each iteration checks if there is something new in database. When it finds a record, it executes the script and everything is fine. The idea is that I launch the backup process from the shell.

Laurențiu Dascălu
  • 2,039
  • 3
  • 22
  • 23