<legend id='BWpZn'><style id='BWpZn'><dir id='BWpZn'><q id='BWpZn'></q></dir></style></legend>
  • <small id='BWpZn'></small><noframes id='BWpZn'>

    <i id='BWpZn'><tr id='BWpZn'><dt id='BWpZn'><q id='BWpZn'><span id='BWpZn'><b id='BWpZn'><form id='BWpZn'><ins id='BWpZn'></ins><ul id='BWpZn'></ul><sub id='BWpZn'></sub></form><legend id='BWpZn'></legend><bdo id='BWpZn'><pre id='BWpZn'><center id='BWpZn'></center></pre></bdo></b><th id='BWpZn'></th></span></q></dt></tr></i><div id='BWpZn'><tfoot id='BWpZn'></tfoot><dl id='BWpZn'><fieldset id='BWpZn'></fieldset></dl></div>

        <bdo id='BWpZn'></bdo><ul id='BWpZn'></ul>

      <tfoot id='BWpZn'></tfoot>
      1. PHP - 在 Cron 中防止碰撞 - 文件锁定安全吗?

        PHP - Preventing collision in Cron - File lock safe?(PHP - 在 Cron 中防止碰撞 - 文件锁定安全吗?)
          <bdo id='wb2Nm'></bdo><ul id='wb2Nm'></ul>
          <i id='wb2Nm'><tr id='wb2Nm'><dt id='wb2Nm'><q id='wb2Nm'><span id='wb2Nm'><b id='wb2Nm'><form id='wb2Nm'><ins id='wb2Nm'></ins><ul id='wb2Nm'></ul><sub id='wb2Nm'></sub></form><legend id='wb2Nm'></legend><bdo id='wb2Nm'><pre id='wb2Nm'><center id='wb2Nm'></center></pre></bdo></b><th id='wb2Nm'></th></span></q></dt></tr></i><div id='wb2Nm'><tfoot id='wb2Nm'></tfoot><dl id='wb2Nm'><fieldset id='wb2Nm'></fieldset></dl></div>

              <tbody id='wb2Nm'></tbody>

            <small id='wb2Nm'></small><noframes id='wb2Nm'>

            <tfoot id='wb2Nm'></tfoot>
              1. <legend id='wb2Nm'><style id='wb2Nm'><dir id='wb2Nm'><q id='wb2Nm'></q></dir></style></legend>
                • 本文介绍了PHP - 在 Cron 中防止碰撞 - 文件锁定安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试寻找一种安全的方法来防止 cron 作业冲突(即,如果另一个实例已经在运行,则阻止它运行).

                  I'm trying to find a safe way to prevent a cron job collision (ie. prevent it from running if another instance is already running).

                  一些我发现推荐的选项 对文件使用锁.

                  这真的是一个安全的选择吗?例如,如果脚本死了会发生什么?锁会保留吗?

                  Is that really a safe option? What would happen if the script dies for example? Will the lock remain?

                  还有其他方法可以做到这一点吗?

                  Are there other ways of doing this?

                  推荐答案

                  此示例取自 http://php.net/flock 并稍作改动,这是一个正确的方式来做你想做的事:

                  This sample was taken at http://php.net/flock and changed a little and this is a correct way to do what you want:

                  $fp = fopen("/path/to/lock/file", "w+");
                  if (flock($fp, LOCK_EX | LOCK_NB)) { // do an exclusive lock
                    // do the work
                    flock($fp, LOCK_UN); // release the lock
                  } else {
                    echo "Couldn't get the lock!";
                  }
                  fclose($fp);
                  

                  不要使用诸如 /tmp/var/tmp 之类的位置,因为它们可能会被您的系统随时清除,因此根据文档弄乱了您的锁:

                  Do not use locations such as /tmp or /var/tmp as they could be cleaned up at any time by your system, thus messing with your lock as per the docs:

                  程序不得假定/tmp 中的任何文件或目录在程序调用之间都保留.

                  Programs must not assume that any files or directories in /tmp are preserved between invocations of the program.

                  https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch03s18.htmlhttps://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch05s15.html

                  务必使用您可以控制的位置.

                  Do use a location that is under your control.

                  学分:

                  • Michal Perrin - 提议使用 w+ 而不是 r+
                  • Michal Perrin - for proposing to use w+ instead of r+

                  这篇关于PHP - 在 Cron 中防止碰撞 - 文件锁定安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                  How to parse django style template tags(如何解析 django 样式模板标签)
                  What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                  How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                  What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                  php in background exec() function(php 后台 exec() 函数)

                    <bdo id='29OhG'></bdo><ul id='29OhG'></ul>
                  • <small id='29OhG'></small><noframes id='29OhG'>

                    1. <legend id='29OhG'><style id='29OhG'><dir id='29OhG'><q id='29OhG'></q></dir></style></legend>
                        <i id='29OhG'><tr id='29OhG'><dt id='29OhG'><q id='29OhG'><span id='29OhG'><b id='29OhG'><form id='29OhG'><ins id='29OhG'></ins><ul id='29OhG'></ul><sub id='29OhG'></sub></form><legend id='29OhG'></legend><bdo id='29OhG'><pre id='29OhG'><center id='29OhG'></center></pre></bdo></b><th id='29OhG'></th></span></q></dt></tr></i><div id='29OhG'><tfoot id='29OhG'></tfoot><dl id='29OhG'><fieldset id='29OhG'></fieldset></dl></div>
                        <tfoot id='29OhG'></tfoot>

                            <tbody id='29OhG'></tbody>