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

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

      1. <small id='Dhc43'></small><noframes id='Dhc43'>

        <tfoot id='Dhc43'></tfoot>
        • <bdo id='Dhc43'></bdo><ul id='Dhc43'></ul>

        如何阻止 cron 作业执行(如果它已经在运行)

        How to prevent the cron job execution, if it is already running(如何阻止 cron 作业执行(如果它已经在运行))
        <i id='IY6nW'><tr id='IY6nW'><dt id='IY6nW'><q id='IY6nW'><span id='IY6nW'><b id='IY6nW'><form id='IY6nW'><ins id='IY6nW'></ins><ul id='IY6nW'></ul><sub id='IY6nW'></sub></form><legend id='IY6nW'></legend><bdo id='IY6nW'><pre id='IY6nW'><center id='IY6nW'></center></pre></bdo></b><th id='IY6nW'></th></span></q></dt></tr></i><div id='IY6nW'><tfoot id='IY6nW'></tfoot><dl id='IY6nW'><fieldset id='IY6nW'></fieldset></dl></div>
        <legend id='IY6nW'><style id='IY6nW'><dir id='IY6nW'><q id='IY6nW'></q></dir></style></legend>

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

              <tfoot id='IY6nW'></tfoot>
                  <tbody id='IY6nW'></tbody>
                  <bdo id='IY6nW'></bdo><ul id='IY6nW'></ul>
                  本文介绍了如何阻止 cron 作业执行(如果它已经在运行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 php 脚本,我在 CentOS 上每 10 分钟通过 cron 执行一次这个脚本.

                  I have one php script, and I am executing this script via cron every 10 minutes on CentOS.

                  问题在于,如果 cron 作业将花费 10 分钟以上,那么同一 cron 作业的另一个实例将启动.

                  The problem is that if the cron job will take more than 10 minutes, then another instance of the same cron job will start.

                  我尝试了一个技巧,那就是:

                  I tried one trick, that is:

                  1. 使用php代码(与pid文件相同)创建了一个锁定文件,当cron 作业开始了.
                  2. 在作业完成后使用 php 代码删除了锁定文件.
                  3. 当任何新的 cron 作业开始执行脚本时,我检查是否锁定文件存在,如果存在,则中止脚本.

                  但是可能会出现一个问题,即锁定文件由于任何原因没有被脚本删除或删除.cron 将永远不会再次启动.

                  But there can be one problem that, when the lock file is not deleted or removed by script because of any reason. The cron will never start again.

                  如果 cron 作业已经在运行,有什么办法可以再次停止它的执行,使用 Linux 命令或类似的命令吗?

                  Is there any way I can stop the execution of a cron job again if it is already running, with Linux commands or similar to this?

                  推荐答案

                  建议锁定正是为此目的而设计的.

                  Advisory locking is made for exactly this purpose.

                  您可以使用 flock() 完成咨询锁定.只需将该函数应用于先前打开的锁定文件,即可确定是否有其他脚本对其进行了锁定.

                  You can accomplish advisory locking with flock(). Simply apply the function to a previously opened lock file to determine if another script has a lock on it.

                  $f = fopen('lock', 'w') or die ('Cannot create lock file');
                  if (flock($f, LOCK_EX | LOCK_NB)) {
                      // yay
                  }
                  

                  在本例中,我添加了 LOCK_NB 以防止下一个脚本等待第一个脚本完成.由于您使用的是 cron,因此总会有下一个脚本.

                  In this case I'm adding LOCK_NB to prevent the next script from waiting until the first has finished. Since you're using cron there will always be a next script.

                  如果当前脚本提前终止,任何文件锁都会被操作系统释放.

                  If the current script prematurely terminates, any file locks will get released by the OS.

                  这篇关于如何阻止 cron 作业执行(如果它已经在运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                      <tbody id='YLzNR'></tbody>
                    <tfoot id='YLzNR'></tfoot>

                    <legend id='YLzNR'><style id='YLzNR'><dir id='YLzNR'><q id='YLzNR'></q></dir></style></legend>
                      • <bdo id='YLzNR'></bdo><ul id='YLzNR'></ul>
                        <i id='YLzNR'><tr id='YLzNR'><dt id='YLzNR'><q id='YLzNR'><span id='YLzNR'><b id='YLzNR'><form id='YLzNR'><ins id='YLzNR'></ins><ul id='YLzNR'></ul><sub id='YLzNR'></sub></form><legend id='YLzNR'></legend><bdo id='YLzNR'><pre id='YLzNR'><center id='YLzNR'></center></pre></bdo></b><th id='YLzNR'></th></span></q></dt></tr></i><div id='YLzNR'><tfoot id='YLzNR'></tfoot><dl id='YLzNR'><fieldset id='YLzNR'></fieldset></dl></div>
                        • <small id='YLzNR'></small><noframes id='YLzNR'>