如何确保我只有一个通过 Apache 运行的 PHP 脚本实例? [英] How can I ensure I have only one instance of a PHP script running via Apache?

查看:30
本文介绍了如何确保我只有一个通过 Apache 运行的 PHP 脚本实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 index.php 脚本,我将其用作 Google 代码站点上的提交后 URL.此脚本克隆一个目录并构建一个可能需要一些工作的项目.我想避免让此脚本并行运行多次.

I have an index.php script that I use as a post-commit URL on a Google Code site. This script clones a directory and builds a project that may take some work. I want to avoid having this script running more than one time in parallel.

如果另一个脚本已经在会话中,我是否可以使用一种机制来避免执行该脚本?

Is there a mechanism I can use to avoid executing that script if another one is already in session?

推荐答案

您可以使用 flockLOCK_EX 获得文件的排他锁.

You can use flock with LOCK_EX to gain an exclusive lock on a file.

例如:

<?php
$fp = fopen('/tmp/php-commit.lock', 'r+');
if (!flock($fp, LOCK_EX | LOCK_NB)) {
    exit;
}

// ... do stuff

fclose($fp);
?>

PHP 5.3.2 以后的版本需要手动释放锁羊群($ fp,LOCK_UN);

For PHP versions after 5.3.2 you need to manually release the lock using flock($fp, LOCK_UN);

这篇关于如何确保我只有一个通过 Apache 运行的 PHP 脚本实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆