• <tfoot id='W57qp'></tfoot>
      <bdo id='W57qp'></bdo><ul id='W57qp'></ul>
      <legend id='W57qp'><style id='W57qp'><dir id='W57qp'><q id='W57qp'></q></dir></style></legend>

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

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

        如何防止 cron 作业执行,如果它已经在运行

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

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

            <bdo id='UgBNo'></bdo><ul id='UgBNo'></ul>
                  <tbody id='UgBNo'></tbody>

                  本文介绍了如何防止 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 的问题)

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

                  • <tfoot id='huA5L'></tfoot>

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