Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP and shell_exec

Tags:

php

debugging

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!

like image 946
Laurențiu Dascălu Avatar asked Jun 04 '26 20:06

Laurențiu Dascălu


2 Answers

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;
like image 190
Brian Avatar answered Jun 06 '26 11:06

Brian


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.

like image 35
Viper_Sb Avatar answered Jun 06 '26 10:06

Viper_Sb