Search code examples
phpshell-execphp-builtin-server

Running a background process with shell_exec from the PHP built-in webserver


The PHP built-in webserver does not seem to handle shell_exec()ing background processes properly; the request hangs until the background process is complete, even if it's explicitly placed in the background with &.

Example:

$ ls
runit.php
$ cat runit.php 
<?php
echo "Here we are\n";
shell_exec("sleep 5 &");
echo "and the command is done\n";
?>
$ php -S localhost:7891
PHP 5.5.9-1ubuntu4.9 Development Server started at Mon May 18 19:20:12 2015
Listening on http://localhost:7891
Press Ctrl-C to quit.

and then in another shell:

$ GET http://localhost:7891/runit.php
(...waits five seconds...)
Here we are
and the command is done

This shouldn't happen, and indeed doesn't if one uses a production-grade webserver. Is there any way to work around it?

(Note: this is not a flushing problem. Adding flush() after the first echo doesn't make it happen, and the request still hangs until the background process is complete.)


Solution

  • This is acknowledged as a bug by PHP, but won't be fixed in the built-in webserver. However, the bug report does also suggest a workaround; set a correct Content-Length on the response, and then the receiving browser will close the request at the client end after receiving that much data, thus working around the problem.