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
";
shell_exec("sleep 5 &");
echo "and the command is done
";
?>
$ 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.)