From the documentation:

In order to execute a command and have it not hang your PHP script while
it runs, the program you run must not output back to PHP. To do this,
redirect both stdout and stderr to /dev/null, then background it.

> /dev/null 2>&1 &

In order to execute a command and have
it spawned off as another process that
is not dependent on the Apache thread
to keep running (will not die if
somebody cancels the page) run this:

exec('bash -c "exec nohup setsid your_command > /dev/null 2>&1 &"');

Answer from Cristian on Stack Overflow
🌐
PHP
php.net › manual › en › function.exec.php
PHP: exec - Manual
This will execute $cmd in the background (no cmd window) without PHP waiting for it to finish, on both Windows and Unix. <?php function execInBackground($cmd) { if (substr(php_uname(), 0, 7) == "Windows"){ pclose(popen("start /B ". $cmd, "r")); } else { exec($cmd .
🌐
Experts Exchange
experts-exchange.com › questions › 28700937 › Execute-PHP-Script-in-a-PHP-Page-without-waiting.html
Solved: Execute PHP Script in a PHP Page without waiting | Experts Exchange
July 28, 2015 - Select allOpen in new window It executes as expected. ... Let's try the "hello world" example. Try saving this as "foo.php" and running it from the command line.
🌐
Subinsb
subinsb.com › how-to-execute-command-without-waiting-for-it-to-finish-in-php
Execute Command Without Waiting For It To Finish - Subin's Blog
function <span style="color: red;">bgExec</span>($cmd) { if(substr(php_uname(), 0, 7) == "Windows"){ pclose(popen("start /B ". $cmd, "r")); }else { exec($cmd .
🌐
SitePoint
sitepoint.com › php
Performing asynchronous php requests from within a script + don't wait for responses - PHP - SitePoint Forums | Web Development & Design Community
December 13, 2008 - Hi, I’ve been searching and searching… but could not come up with something interesting. From within a script, I’d like to perform a couple of HTTP requests to external ressources. However, I’d like this script to execute as fast as possible and some of these external ressources may ...
🌐
PHP Classes
phpclasses.org › package › 9674-PHP-Get-commands-output-without-waiting-to-finish.html
PHP Asynchronous Command: Get commands output without waiting to finish - PHP Classes
April 2, 2016 - This class can get commands output without waiting for them to be finished. It can execute external commands asynchronously and get their output on demand using pipes, even before the command have finished its execution. The class can perform other operations like changing the current directory ...
Find elsewhere
🌐
Jackmarchant
jackmarchant.com › exploring-async-php
Exploring Async PHP | Jack Marchant
May 31, 2023 - The above scripts show the web request still finishes in milliseconds, even though there is a blocking sleep function call in the send_email.php script. The reason it doesn't block is because we've told exec with the inclusion of > /dev/null & in the command that we don't want to wait for exec command to finish so we can get the result, meaning it can happen in the background and the web request can continue.
🌐
PHPBuilder
board.phpbuilder.com › d › 10351142-how-can-i-exec-in-a-non-blocking-fashion
How can I exec() in a non-blocking fashion? - PHPBuilder Forums
February 15, 2008 - Within my PHP script, I want to do an exec(), but I don't want PHP to wait for the console to finish executing the program. I just want to start the program...
🌐
Coding Sips
codingsips.com › home › php echo without waiting to finish execution
php echo without waiting to finish execution - Coding Sips
July 22, 2017 - And now try the following code and glance at result it will show output at each iteration of loop do not waiting to complete the execution. We can also write multiple loops to display instant result without waiting for completing the execution of whole script.