<bdo id='6Gu1z'></bdo><ul id='6Gu1z'></ul>

    <tfoot id='6Gu1z'></tfoot>

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

      1. <small id='6Gu1z'></small><noframes id='6Gu1z'>

        <legend id='6Gu1z'><style id='6Gu1z'><dir id='6Gu1z'><q id='6Gu1z'></q></dir></style></legend>
      2. 如何防止PHP脚本多次运行?

        How to prevent PHP script running more than once?(如何防止PHP脚本多次运行?)
        <i id='bBqz7'><tr id='bBqz7'><dt id='bBqz7'><q id='bBqz7'><span id='bBqz7'><b id='bBqz7'><form id='bBqz7'><ins id='bBqz7'></ins><ul id='bBqz7'></ul><sub id='bBqz7'></sub></form><legend id='bBqz7'></legend><bdo id='bBqz7'><pre id='bBqz7'><center id='bBqz7'></center></pre></bdo></b><th id='bBqz7'></th></span></q></dt></tr></i><div id='bBqz7'><tfoot id='bBqz7'></tfoot><dl id='bBqz7'><fieldset id='bBqz7'></fieldset></dl></div>
          <bdo id='bBqz7'></bdo><ul id='bBqz7'></ul>

              <tbody id='bBqz7'></tbody>

            <tfoot id='bBqz7'></tfoot>

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

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

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

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

                  相关文档推荐

                  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() 函数)

                    <legend id='TwLjP'><style id='TwLjP'><dir id='TwLjP'><q id='TwLjP'></q></dir></style></legend>

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

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

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

                          <tfoot id='TwLjP'></tfoot>