How can I run PHP code asynchronously without waiting? I have a long run (almost infinite) that should run while server starts and should process asynchronously without wait
How Can I run PHP code asynchronously without waiting. I have a long run (almost inifinite) that should run while server starts and should process asynchrously without waiting.
Assuming a typical LAMP system, you can start a PHP daemon from the command line with
root# php yourscript.php &
where yourscript.php contains something similar to
Embellishments: To make your script directly executable: chmod +x yourscript.php and add #!/usr/bin/php to the beginning of yourscript
To start with apache, you should add that command to your apache startup script (usually apachectl) and be sure to add code to kill it when apache stops.
The check if you are already running involves a file with your PID in /var/locks/ and something like system('/bin/ps '.$thePID); It also makes the kill instruction easier to write.