• <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 脚本多次运行?

        How to prevent PHP script running more than once?(如何防止 PHP 脚本多次运行?)
        • <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 脚本多次运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Converting string to MySQL timestamp format in php(在php中将字符串转换为MySQL时间戳格式)
                  datetime to timestamp(日期时间到时间戳)
                  PHP timestamp date to user timezone(PHP时间戳日期到用户时区)
                  Converting TIMESTAMP to unix time in PHP?(在 PHP 中将 TIMESTAMP 转换为 unix 时间?)
                  Convert ISO 8601 to unixtimestamp(将 ISO 8601 转换为 unixtimestamp)
                  Finding days between 2 unix timestamps in php(在php中查找2个unix时间戳之间的天数)
                  <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'>

                        • <legend id='KA0PF'><style id='KA0PF'><dir id='KA0PF'><q id='KA0PF'></q></dir></style></legend>

                            <tbody id='KA0PF'></tbody>

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