如何防止cron作业执行,如果它已经在运行 [英] How to prevent the cron job execution, if it is already running

查看:115
本文介绍了如何防止cron作业执行,如果它已经在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

问题是,如果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. 在cron作业开始时,使用php代码(与pid文件相同)创建了一个锁定文件。

  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?

推荐答案

(编辑)我的建议只发布为解决方案case flock

我已经使用 flock() 但由于某些原因没有停止多个实例cron脚本。所以经过一番搜索后,我发现这个类,并稍微修改它自己使用:

I had tested using flock() but for some reason that didn't stop multiple instances of the cron script. So after a little search I found this class and modified it slightly for my own use:

http://abhinavsingh.com/blog/2009/12/how- to-use-locks-in-php-cron-jobs-to-avoid-cron-overlaps /

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆