Im having a little trouble with running a php script in the background.
I have my admin area which has HTTP authentication to access and a mail out script which I want to run in the background. This script will take a long time to execute so I wish it to execute in the background.
My idea is when I access the "send" page, it executes the send script in the background and redirects the user off elsewhere.
However currently attempting to use cURL I cannot cURL into the send script as it returns Authorization required.
Any advice appreciated, thanks.
You might try pcntl_fork() if you're running it on Linux. Note that this approach requires some magic on the PHP installation part (pcntl is disabled by default for cgi) and even more magic to make your script survive apache process cleanup. See this comment on PHP to get you started.
So forking off a process would look like this:
if ($pid = pcntl_fork())
die(); // Parent
function shutdown() {
posix_kill(posix_getpid(), SIGHUP);
}
ob_end_clean(); // Discard the output buffer and close
fclose(STDIN); // Close all of the standard
fclose(STDOUT); // file descriptors as we
fclose(STDERR); // are running as a daemon.
register_shutdown_function('shutdown');
if (posix_setsid() < 0)
die(); // <- This is an error
if ($pid = pcntl_fork())
die(); // Parent
// Do your stuff here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With