How to run the PHP code asynchronous

前端 未结 5 1425
小蘑菇
小蘑菇 2020-11-27 06:55

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

5条回答
  •  生来不讨喜
    2020-11-27 07:32

    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.

提交回复
热议问题