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!

4
  • 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 stackoverflow.com/questions/45953/… Commented Nov 18, 2010 at 11:04
  • - he said he is using shell_exec - so i assume he he is using his script as a commandline tool Commented Nov 18, 2010 at 11:07
  • the script is a commandline tool Commented Nov 18, 2010 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 &") Commented Nov 18, 2010 at 14:51

6 Answers 6

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;
Sign up to request clarification or add additional context in comments.

1 Comment

It also returns null on error which is not very helpful
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.

Comments

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.

3 Comments

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)
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?)
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).
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/

1 Comment

aww sry misread the topic but maybe it helps if your script runs to long
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.

Comments

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.

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.