PHP - Restrict cron job overlap with flock()

南笙酒味 提交于 2019-12-08 13:16:35

Your Code goes here:

$fp = fopen('/tmp/lock.txt', 'w');

if(!flock($fp, LOCK_EX | LOCK_NB)) {
    echo 'Unable to obtain lock';
    exit(-1);
}

// YOUR CODE HERE    
sleep(5);

fclose($fp);

lock.txt just holds your lock. You need write access to this file to create it in the first place. And use a unique name for your locking file, so it doesn't interfere with other processes.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!