Having read loads of threads about calling a long-running PHP CLI script from a web script, and the best way to daemonize the CLI script, I have hit an insoluble problem with the way Apache handles the CLI script. According to this blog post, the best option is to do something like :
print `echo /usr/bin/php -q longThing.php | at now`;
When I do this on my local CentOS 7/PHP 7/Apache 2.4.6 VM, it works like a charm, however, the identical code on my production server Centos 6.8/PHP 7/Apache 2.4.25 (cPanel) causes Apache to spawn 100s of child processes, rapidly exhausting the CPU and memory resources, requiring a reboot to fix. Even if I quickly kill the PHP CLI process, Apache has already got itself into some kind of loop. My understanding was that by using the suggested code above, you disassociated Apache from the CLI process, but clearly not. Can anyone point me toward what is going wrong ? I can post the actual code and more details of my production server if needed. Thanks
Test this:
php file called xyz.php
<?php
shell_exec('php -r "sleep(20);" >> /dev/null 2>&1 &');
then run under command line
php xyz.php
And it is equal behavior if you call it from an browser.
xyz.php
will finish and not wait 20 seconds.