Why does PHP hang when a bash call wiki

I am calling bash code from php, but even if I create bash (with & ), php will not exit until the complete bash is complete.

This code:

 <html> <body> HTML START<br> <pre> <?php echo "PHP START\n"; echo `sleep 30 &`; echo "PHP END\n"; ?> </pre> HTML END<br> </body> </html> 

Do not show anything in the browser, and then after 30 seconds.

I really want to run a graphical application from php, which should continue.

+4
source share
5 answers

Close all file descriptors during a sleep call so that it can disconnect:

 <?php echo "PHP START\n"; echo `sleep 30 <&- 1<&- 2<&- &`; echo "PHP END\n"; ?> 

Otherwise, the output file descriptor is still open, and PHP is still trying to wait to receive its output, even if the process is no longer directly bound.

This works correctly at startup, leaving the system immediately, but leaving the sleep process behind:

 $ time php5 test.php; ps auxw | grep sleep | grep -v grep PHP START PHP END real 0m0.019s user 0m0.008s sys 0m0.004s cduffy 6239 0.0 0.0 11240 576 pts/0 S 11:23 0:00 sleep 30 
+3
source

PHP expects the called process to terminate, even if amperous explicitly

+1
source

Perhaps create another HTTP process using an Ajax call to another PHP script, the task of which will be to run your sleep 30 command or anything on the server asynchronously.

0
source

what you probably want to do is split up the forked processes. One way to do this is to use nohup in the bash command.

for example, consider user comments on php.net> exec, especially this one: http://www.php.net/manual/en/function.exec.php#88704

Be careful, however, you lose direct feedback and must monitor your processes / results using other means.

0
source
 sleep(30); 

mast...

 <?php echo "PHP START\n"; echo sleep(30); echo "PHP END\n"; ?> 
-1
source

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


All Articles