How to detect whether a PHP script is already running?

后端 未结 9 1347
借酒劲吻你
借酒劲吻你 2021-01-31 11:26

I have a cron script that executes a PHP script every 10 minutes. The script checks a queue and processes the data in the queue. Sometimes the queue has enough data to last ov

9条回答
  •  逝去的感伤
    2021-01-31 11:42

    If you need it to be absolutely crash-proof, you should use semaphores, which are released automatically when php ends the specific request handling.

    A simpler approach would be to create a DB record or a file at the beginning of the execution, and remove it at the end. You could always check the "age" of that record/file, and if it's older than say 3 times the normal script execution, suppose it crashed and remove it.

    There's no "silver bullet", it just depends on your needs.

提交回复
热议问题