8

I have the following which executes perfectly but NOT in the background as it should? It actually stops the page loading until it finishes which is not good.

shell_exec("/usr/bin/php /home/public_html/pages/test/backg.php {$user_info} {$user_info2} {$user_info3} &");

I also tried

exec("/usr/bin/php /home/public_html/pages/test/backg.php {$user_info} {$user_info2} {$user_info3} &");

I thought the

&

meant it would execute then let the holding page carry on regardless?

StudioTime
  • 22,603
  • 38
  • 120
  • 207
  • In my expierence exec() always takes like 0.2seconds to execute. Regardless what command you execute. If you put sleep(10); in your `backg.php` file, will it take 10 seconds longer to execute the original page? – Sietse May 06 '12 at 17:49
  • @Sietse this is taking the full length of the backg.php file to execute... its behaving more like an include as opposed to an exec script which is supposed to be executed in the background – StudioTime May 06 '12 at 18:22
  • You can look at my answer I posted here: http://stackoverflow.com/questions/7703278/grab-results-from-a-php-exec-while-the-command-is-still-running/7703323#7703323 Maybe that can help. – Sietse May 06 '12 at 19:58

1 Answers1

13
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));

php execute a background process

exec("/usr/bin/php /path/background.php > /dev/null 2>&1 &");
semsem
  • 1,194
  • 12
  • 24
  • It is a very old answer but save me today in 2021 to run a Laravel task in background using Apache2 and mod_php that do not supprt dispatchAfterResponse (doesnt run assync) – Marcos Regis Mar 20 '21 at 17:58