简体   繁体   English

防止计划的 cron 作业在失败时重复

[英]Keep a scheduled cron job from repeating on failure

I have a cron job that runs every minute that involves logging into a database.我有一个每分钟运行一次的 cron 作业,涉及登录数据库。 Our organization mandates we change our passwords every 6 months - and the password change process can be pretty laggy - sometimes taking up to 10 minutes.我们的组织要求我们每 6 个月更改一次密码 - 密码更改过程可能非常缓慢 - 有时需要长达 10 分钟。 However, during the latency period between when the password is changed via our tool (and goes into effect) and the change can be made in hard coded instances on our Linux box, this job will incur several failed login attempts to the database.但是,在通过我们的工具更改密码(并生效)和可以在我们的 Linux 机器上的硬编码实例中进行更改之间的延迟期间,此作业将导致多次登录数据库失败。 Once it gets past a certain number, the account is locked - so all our subsequent jobs fail until we can get in touch with the DBA to unlock the account.一旦超过某个数字,该帐户就会被锁定 - 因此我们所有后续的工作都会失败,直到我们可以与 DBA 联系以解锁该帐户。

All the questionable practices demonstrated here notwithstanding (hard coded passwords, etc.) - I'm looking for a means for the cron job to essentially withdraw itself from the schedule if/when the login fails.尽管这里展示了所有有问题的做法(硬编码密码等) - 我正在寻找一种方法,让 cron 作业在登录失败时从计划中退出。 Yes, doing so manually prior to the password change is possible - but not everyone going through this process is terribly linux/cron/vi literate - hence my search for some form of silver bullet that might avoid this if possible.是的,在更改密码之前手动执行此操作是可能的 - 但并非每个经历此过程的人都非常了解 linux/cron/vi - 因此我寻找某种形式的灵丹妙药,如果可能的话,可能会避免这种情况。

Any suggestions much appreciated.任何建议非常感谢。

To prevent a cron job from failing more than once, you need a semaphore file.为了防止 cron 作业多次失败,您需要一个信号量文件。 This solution is similar to the comment by tvm .此解决方案类似于tvm的评论。 Create the semaphore file outside of the cron job like so:在 cron 作业之外创建信号量文件,如下所示:

touch is_ok.txt

It needs to be recreated after every failure to enable the cron job to run.每次失败后需要重新创建它才能使 cron 作业运行。 Then wrap your command like so:然后像这样包装你的command

if [[ -e is_ok.txt ]] && command ; then touch is_ok.txt ; else rm -f is_ok.txt ; fi

Example (using grep foo bar as your cron job command ):示例(使用grep foo bar作为您的 cron 作业command ):

# Enable `grep foo bar` to run:

touch is_ok.txt

# Create the input for `grep foo bar` to run successfully:

echo foo > bar

# `grep foo bar` runs successfully repeatedly:

if [[ -e is_ok.txt ]] && grep foo bar ; then touch is_ok.txt ; else rm -f is_ok.txt ; fi

# Make `grep foo bar` fail:

rm bar

# `grep foo bar` runs once, then never runs until you do `touch is_ok.txt`:

if [[ -e is_ok.txt ]] && grep foo bar ; then touch is_ok.txt ; else rm -f is_ok.txt ; fi

Depending on your box, OS, shell, etc ... this might and might not work.取决于您的机器、操作系统、外壳等……这可能会也可能不会起作用。

For bash for instance, you might want to change the command run by cron to something like this :例如,对于 bash,您可能希望将 cron 运行的命令更改为如下所示:

*/1 * * * * Command || */1 * * * * 命令 || exit出口

Which basically means : Every minute, run this command OR exit.这基本上意味着:每分钟,运行此命令或退出。 so, if the command fails, it will exit.因此,如果命令失败,它将退出。 but that will still be logged as a failed attempt to login.但这仍会被记录为登录尝试失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM