PHP and shell_exec

I have a PHP site and I would like to execute a very long Python script in the background (300 MB of memory and 100 seconds). The process connection is done through the database: when the Python script completes its task, it updates the field in the database, and then the website displays some graphs based on the results of the Python script.

I can execute a “manually” Python script from bash (any current directory) and it works. I would like to integrate it into PHP, and I tried the shell_exec function:

shell_exec("python /full/path/to/my/script") but it does not work (I do not see any output)

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

Thanks!

+1
source share
6 answers

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

I wrote a backup process that runs forever and checks at every iteration if there is anything new in the database. When he finds the record, he runs the script, and everything is fine. The idea is that I start the backup process from the shell.

0
source

shell_exec returns a string, if you run it yourself, it will not produce any output, so you can write:

 $output = shell_exec(...); print $output; 
+3
source

First set_time_limit (0); will make your script run forever, so timeout should not be a problem. The second call to any * exec in PHP does NOT use PATH by default (it may depend on the configuration), so your script will exit without providing any information about the problem, and quite often it turns out that it cannot find programs, in this case python. Therefore, change it to:

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

If your python script runs on it on its own without problems, then this is most likely a problem. As far as memory is concerned, I'm sure PHP will not use the same memory as python. Therefore, if it uses 300 MB of PHP, it should remain at the default level (say, 1 MB) and just wait for shell_exec to finish.

+1
source

It may be that your script takes longer to wait for the server defined for the request (it can be installed in php.ini or httpd.conf).

Another problem may be that the server account does not have the right to execute or access the code or files necessary to run the script.

0
source

This was discovered earlier and helped me solve the problem with background execution:

 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/

0
source

I found that the problem when I tried this was the simple fact that I did not compile the source on the server on which I ran it. Compiling to your local computer, and then downloading it to your server, it will somehow be damaged. shell_exec () should work by compiling the source that you are trying to run on the same server the script is running on.

0
source

Source: https://habr.com/ru/post/1501486/


All Articles