php 限制LInux的某个进程脚本数量

我们在Linux环境中需要运行定时脚本数量,同一个脚本同一时间只能运行一个,我们可以用下面的函数进行限制。比如再yii框架中:funCheckThread('kbfg create_worker')

    /**
     * 限制脚本运行个数
     * @param       string  $strName                需要检测的脚本名称
     * @param       int             $intNum                 默认不超过的线程数1个(不包含)
     * @return  boolean                                     true:可以继续   false:已经达到最大脚本限制
     */
    function funCheckThread($strName = '', $intNum = 1) {
        set_time_limit(0);

        // 定义将要运行的语句
        $strExec = '';
        $isReturn = true;

        $strExec = "ps -ef | grep php | grep '{$strName}' | grep -v grep | grep -v '>>' | wc -l";
        $count = exec($strExec);
        echo "进程数:" . ($count - 1) . "\tpid:" . getmypid() . "\n";
        if ($count > $intNum) {
            $isReturn = false;
        }

        return $isReturn;
    }

本函数仅能在Linux环境中运行。


评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注