如何设置一个系统来告诉我 cron 作业是否运行正常?

发布于 2024-09-14 00:29:22 字数 211 浏览 10 评论 0原文

这更多的是一个“通用架构”问题。如果您有一个定期运行的 cron 作业(甚至是 Windows 计划任务),那么让它向您发送一封电子邮件/短信表明一切正常,这有点简单,但是当一切正常时我如何得到通知不好吗?基本上,如果作业没有在预定时间运行,或者 Windows / Linux 有自己的一组挂起阻止任务运行......?

只是寻求以前遇到过这种情况的人的想法并提出有趣的解决方案......

This is more of an "general architecture" problem. If you have a cron job (or even a Windows scheduled task) running periodically, its somewhat simple to have it send you an email / text message that all is well, but how do I get informed when everything is NOT okay? Basically, if the job doesn't run at its scheduled time or Windows / linux has its own set of hangups that prevent the task from running...?

Just seeking thoughts of people who've faced this situation before and come up with interesting solutions...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必在意 2024-09-21 00:29:22

我过去这样做的一种方法是简单地放在每个脚本的顶部(例如 checkUsers.sh):

touch /tmp/lastrun/checkUsers.sh

然后使用 find 定期运行另一个作业code> 来查找 tmp/lastrun 中超过一天的所有“标记”文件。

您可以调整时间,使用 /tmp/lastrun/hour/tmp/lastrun/day/ 来分隔具有不同计划的作业。

请注意,这不会捕获从未运行的脚本,因为它们永远不会为find创建初始文件。为了缓解这种情况,您可以:

  • 在创建 cron 作业时手动创建该文件(不会处理有人无意中删除标记文件的情况);或者
  • 在某处维护所需标记文件的列表,以便您可以检测它们何时丢失或过时。

而且,如果您的 cron 作业不是脚本,请将 touch 直接放入 crontab 中:

0 4 * * * ( touch /tmp/lastrun/daily/checkUsers ; /usr/bin/checkUsers )

验证简单的 find 脚本比验证简单得多验证您的每一项 cron 作业。

A way I've done it in the past is to simply put at the top of each script (say, checkUsers.sh):

touch /tmp/lastrun/checkUsers.sh

then have another job that runs periodically that uses find to locate all those "marker" files in tmp/lastrun that are older than a day.

You can fiddle with the timings, having /tmp/lastrun/hour/ and tmp/lastrun/day/ to separate jobs that have different schedules.

Note that this won't catch scripts that have never run since they will never create the initial file for find-ing. To alleviate that, you can either:

  • create that file manually when creating the cron job (won't handle situations where someone inadvertently deletes the marker file); or
  • maintain a list of required marker files somewhere so that you can detect when they're missing as well as outdated.

And, if your cron job is not a script, put the touch directly into crontab:

0 4 * * * ( touch /tmp/lastrun/daily/checkUsers ; /usr/bin/checkUsers )

It's a lot easier to validate a simple find script than to validate every one of your cron jobs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文