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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10552016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:27:06  来源:igfitidea点击:

How to prevent the cron job execution, if it is already running

phpcroncentos

提问by Sanjay

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

我有一个 php 脚本,我在 CentOS 上每 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.

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

I tried one trick, that is:

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

  1. Created one lock file with php code (same like pid files) when the cron job started.
  2. Removed the lock file with php code when the job finished.
  3. And when any new cron job started execution of script, I checked if lock file exists and if so, aborted the script.
  1. 在 cron 作业开始时,使用 php 代码(与 pid 文件相同)创建了一个锁定文件。
  2. 作业完成后,用 php 代码删除了锁定文件。
  3. 当任何新的 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 将永远不会再次启动。

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?

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

回答by Ja?ck

Advisory locking is made for exactly this purpose.

咨询锁定正是为此目的而设计的。

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.

您可以使用flock(). 只需将该函数应用于先前打开的锁定文件,以确定另一个脚本是否对其具有锁定。

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

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

在这种情况下,我添加LOCK_NB以防止下一个脚本等待第一个脚本完成。由于您使用的是 cron,因此总会有下一个脚本。

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

如果当前脚本过早终止,操作系统将释放任何文件锁定。

回答by Leonid Shagabutdinov

Maybe it is better to not write code if you can configure it:

如果可以配置的话,也许不写代码会更好:

https://serverfault.com/questions/82857/prevent-duplicate-cron-jobs-running

https://serverfault.com/questions/82857/prevent-duplicate-cron-jobs-running

回答by mgapatrick

flock()worked out great for me - I have a cron job with database requests scheduled every 5 minutes, so not having several running at the same time is crucial. This is what I did:

flock()对我来说很好 - 我有一个 cron 作业,每 5 分钟安排一次数据库请求,所以不要同时运行几个是至关重要的。这就是我所做的:

$filehandle = fopen("lock.txt", "c+");

if (flock($filehandle, LOCK_EX | LOCK_NB)) {
    // code here to start the cron job
   flock($filehandle, LOCK_UN);  // don't forget to release the lock
} else {
    // throw an exception here to stop the next cron job
}

fclose($filehandle);

In case you don't want to kill the next scheduled cron job, but simply pause it till the running one is finished, then just omit the LOCK_NB:

如果您不想终止下一个计划的 cron 作业,而只是暂停它直到正在运行的作业完成,那么只需省略LOCK_NB

if (flock($filehandle, LOCK_EX)) 

回答by coderofsalvation

This is a very common problem with a very simple solution: cronjoblocka simple 8-lines shellscript wrapper applies locking using flock:

这是一个非常简单的解决方案的常见问题:cronjoblock一个简单的 8 行 shellscript 包装器使用 flock 应用锁定:

https://gist.github.com/coderofsalvation/1102e56d3d4dcbb1e36f

https://gist.github.com/coderofsalvation/1102e56d3d4dcbb1e36f

btw. cronjoblockalso reverses cron's spammy emailbehaviour: only output something if stuff goes wrong. This is handy in respect to cron's MAILTO variable. The stdout/stderr output will be suppressed (so cron will not send mails) unlessthe given process has an exitcode > 0

顺便提一句。cronjoblock还逆转了 cron 的垃圾邮件行为:只有在出现问题时才输出一些东西。这对于 cron 的 MAILTO 变量很方便。除非给定进程的退出代码 > 0,否则stdout/stderr 输出将被抑制(因此 cron 不会发送邮件)

回答by Growling Flea

I was running a php cron job script that dealt specifically with sending text messages using an existing API. On my local box the cron job was working fine, but on my customer's box it was sending double messages. Although this doesn't make sense to me, I double checked the permissions for the folder responsible for sending messages and the permission was set to root. Once I set the owner as www-data (Ubuntu) it started behaving normally.

我正在运行一个 php cron 作业脚本,专门处理使用现有 API 发送文本消息。在我的本地机器上,cron 工作运行良好,但在我客户的机器上,它发送了双重消息。虽然这对我来说没有意义,但我仔细检查了负责发送消息的文件夹的权限,并将权限设置为 root。一旦我将所有者设置为 www-data (Ubuntu),它就开始正常运行。

This might mot be the issue for you, but if its a simple cron script I would double check the permissions.

这对您来说可能不是问题,但如果它是一个简单的 cron 脚本,我会仔细检查权限。

回答by Ganesh Bora

flock will not work in php 5.3.3 as The automatic unlocking when the file's resource handle is closed was removed. Unlocking now always has to be done manually.

flock 在 php 5.3.3 中不起作用,因为文件的资源句柄关闭时的自动解锁被删除。现在解锁总是必须手动完成。

回答by Dragos

I use this ::

我用这个::

<?php
// Create a PID file
if (is_file (dirname ($_SERVER['SCRIPT_NAME']) . "/.processing")) { die (); }
file_put_contents (dirname ($_SERVER['SCRIPT_NAME']) . "/.processing", "processing");

// SCRIPT CONTENTS GOES HERE //

@unlink (dirname ($_SERVER['SCRIPT_NAME']) . "/.processing");
?>

回答by Ashwini Kumar Maurya

#!/bin/bash
ps -ef | grep -v grep | grep capture_12hz_sampling_track.php
if [ $? -eq 1 ];
then
     nohup /usr/local/bin/php /opt/Apache/htdocs/cmsmusic_v2/script/Mp3DownloadProcessMp4/capture_12hz_sampling_track.php &
else
      echo "Already running"
fi

回答by joseantgv

Another alternative:

另一种选择:

<?php

/**
* Lock manager to ensure our cron doesn't run twice at the same time.
*
* Inspired by the lock mechanism in Mage_Index_Model_Process
*
* Usage:
* 
* $lock = Mage::getModel('stcore/cron_lock');
*
* if (!$lock->isLocked()) {
*      $lock->lock();
*      // Do your stuff
*      $lock->unlock();
* }
*/
class ST_Core_Model_Cron_Lock extends Varien_Object
{
    /**
     * Process lock properties
     */
    protected $_isLocked = null;
    protected $_lockFile = null;

    /**
     * Get lock file resource
     *
     * @return resource
     */
    protected function _getLockFile()
    {
        if ($this->_lockFile === null) {
            $varDir = Mage::getConfig()->getVarDir('locks');
            $file = $varDir . DS . 'stcore_cron.lock';
            if (is_file($file)) {
                $this->_lockFile = fopen($file, 'w');
            } else {
                $this->_lockFile = fopen($file, 'x');
            }
            fwrite($this->_lockFile, date('r'));
        }
        return $this->_lockFile;
    }

    /**
     * Lock process without blocking.
     * This method allow protect multiple process runing and fast lock validation.
     *
     * @return Mage_Index_Model_Process
     */
    public function lock()
    {
        $this->_isLocked = true;
        flock($this->_getLockFile(), LOCK_EX | LOCK_NB);
        return $this;
    }

    /**
     * Lock and block process.
     * If new instance of the process will try validate locking state
     * script will wait until process will be unlocked
     *
     * @return Mage_Index_Model_Process
     */
    public function lockAndBlock()
    {
        $this->_isLocked = true;
        flock($this->_getLockFile(), LOCK_EX);
        return $this;
    }

    /**
     * Unlock process
     *
     * @return Mage_Index_Model_Process
     */
    public function unlock()
    {
        $this->_isLocked = false;
        flock($this->_getLockFile(), LOCK_UN);
        return $this;
    }

    /**
     * Check if process is locked
     *
     * @return bool
     */
    public function isLocked()
    {
        if ($this->_isLocked !== null) {
            return $this->_isLocked;
        } else {
            $fp = $this->_getLockFile();
            if (flock($fp, LOCK_EX | LOCK_NB)) {
                flock($fp, LOCK_UN);
                return false;
            }
            return true;
        }
    }

    /**
     * Close file resource if it was opened
     */
    public function __destruct()
    {
        if ($this->_lockFile) {
            fclose($this->_lockFile);
        }
    }
}

Source: https://gist.github.com/wcurtis/9539178

来源:https: //gist.github.com/wcurtis/9539178