How to detect whether a PHP script is already running?

后端 未结 9 1505
借酒劲吻你
借酒劲吻你 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:40

    Assuming this is a linux server and you have cronjobs available

    ///Check for running script and run if non-exist///
    
    #! /bin/bash
    
    check=$(ps -fea | grep -v grep | grep script.php | wc -l)
    date=$(date +%Y-%m%d" "%H:%M:%S)
    if [ "$check" -lt 1 ]; then
    echo "["$date"] Starting script" >> /path/to/script/log/
    /sbin/script  ///Call the script here - see below///
    fi
    

    script file

    #/usr/bin/php /path/to/your/php/script.php
    

提交回复
热议问题