• <small id='38FQ0'></small><noframes id='38FQ0'>

      <tfoot id='38FQ0'></tfoot>

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

        如何防止 PHP 脚本多次运行?

        时间:2024-04-13
        • <legend id='s3pKI'><style id='s3pKI'><dir id='s3pKI'><q id='s3pKI'></q></dir></style></legend>

        • <small id='s3pKI'></small><noframes id='s3pKI'>

            <tbody id='s3pKI'></tbody>

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

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

              <tfoot id='s3pKI'></tfoot>
                  本文介绍了如何防止 PHP 脚本多次运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  目前,我试图阻止 onlytask.php 脚本多次运行:

                  Currently, I tried to prevent an onlytask.php script from running more than once:

                  $fp = fopen("/tmp/"."onlyme.lock", "a+");
                  if (flock($fp, LOCK_EX | LOCK_NB)) {
                    echo "task started
                  ";
                    //
                      while (true) {
                        // do something lengthy
                        sleep(10);
                      }
                    //
                    flock($fp, LOCK_UN);
                  } else {
                    echo "task already running
                  ";
                  }
                  fclose($fp);
                  

                  并且有一个cron作业每分钟执行一次上述脚本:

                  and there is a cron job to execute the above script every minute:

                  * * * * * php /usr/local/src/onlytask.php
                  

                  它工作了一段时间.几天后,当我这样做时:

                  It works for a while. After a few day, when I do:

                  ps auxwww | grep onlytask
                  

                  我发现有 两个 实例正在运行!不是三个或更多,不是一个.我杀死了其中一个个体.几天后,又有两个实例.

                  I found that there are two instances running! Not three or more, not one. I killed one of the instances. After a few days, there are two instances again.

                  代码有什么问题?是否有其他替代方法可以限制 onlytask.php 的一个实例正在运行?

                  What's wrong in the code? Are there other alternatives to limit only one instance of the onlytask.php is running?

                  附言我的 /tmp/ 文件夹未清理.ls -al/tmp/*.lock 显示锁定文件是在第一天创建的:

                  p.s. my /tmp/ folder is not cleaned up. ls -al /tmp/*.lock show the lock file was created in day one:

                  -rw-r--r--  1 root root    0 Dec  4 04:03 onlyme.lock
                  

                  推荐答案

                  现在我通过 ps 检查进程是否正在运行,并通过 bash 脚本扭曲 php 脚本:

                  Now I check whether the process is running by ps and warp the php script by a bash script:

                   #!/bin/bash
                  
                   PIDS=`ps aux | grep onlytask.php | grep -v grep`
                   if [ -z "$PIDS" ]; then
                       echo "Starting onlytask.php ..."
                       php /usr/local/src/onlytask.php >> /var/log/onlytask.log &
                   else
                       echo "onlytask.php already running."
                   fi
                  

                  并通过 cron 每分钟运行一次 bash 脚本.

                  and run the bash script by cron every minute.

                  这篇关于如何防止 PHP 脚本多次运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHP 警告:未知:输入变量超过 1000 下一篇:PHP require_once 无法打开流权限被拒绝

                  相关文章

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

                  • <bdo id='KA0PF'></bdo><ul id='KA0PF'></ul>

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

                  1. <legend id='KA0PF'><style id='KA0PF'><dir id='KA0PF'><q id='KA0PF'></q></dir></style></legend>

                      <tfoot id='KA0PF'></tfoot>