如果当前作业未完成,Cron 会开始新作业吗? [英] Will Cron start a new job if the current job is not complete?

查看:16
本文介绍了如果当前作业未完成,Cron 会开始新作业吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何防止cron作业执行,如果它已经在运行

我有一个 cron 作业,可能需要 2 分钟或 5 小时才能完成.我需要确保始终执行此作业.

I have a cron job that may take 2 mins or may take 5 hours to complete. I need to make sure that this job is always executed.

我的问题是:

如果我将它设置为每分钟执行一次,它会在前一个完成后启动还是它们同时运行并弄乱数据库?

Will it start after the previous one is done or will they both run at the same time and mess up the database if i set it to execute every minute ?

推荐答案

它们将同时运行.围绕此的标准做法是创建一个文件锁(通常称为 flock),如果该锁不可用,则不要运行.

They'll run at the same time. The standard practice around this is to create a file lock (commonly referred to as a flock), and if the lock isn't available, don't run.

与 Zdenek 的方法相比,这种方法的优势在于它不需要数据库,并且当该过程结束时,会自动释放群.这包括进程被终止、服务器重新启动等情况.

The advantages to this over Zdenek's approach is that it doesn't require a database, and when the process ends, the flock is automatically released. This includes cases where the the process is killed, server rebooted, etc.

虽然您没有提到您的 cron 作业是用什么编写的,但是 flock 在大多数语言中都是标准的.我建议谷歌搜索锁和你使用的语言,因为这将是更强大的解决方案,而不是依赖于随机超时.以下是 SO 的一些示例:

While you don't mention what your cron job is written in, flock is standard in most languages. I'd suggest googling for locks and the language you're using, as this will be the more robust solution, and not relying upon random timeouts. Here are some examples from SO:

这篇关于如果当前作业未完成,Cron 会开始新作业吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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