查找在给定时间之间运行的 cron 作业
是否可以找到 crontab 中在时间 X 和时间 Y 之间运行的所有条目,而不必自己解析 cron 时间条目?我主要关心时间小时和分钟,而不关心其他 3 个时间字段。
Is it possible to find all entries in a crontab that run between time X and time Y without having to parse the cron time entries myself? I'm mainly concerned with time hour and minute, not so much the other 3 time fields.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一切皆有可能,但你必须自己解析 crontab。
没有简单的答案,但只是因为我可以在 bash 中找到部分解决方案。
像这样的调用
当然不适用于复杂的时间规范(例如*/2),并且仅报告命令的第一部分。所有这些都可以纠正,但可能你最好用 perl 或其他东西来做。
Anything is possible but you will have to parse crontab yourself.
There are no simple answers, but just because I can here's a partial solution in bash.
Call like
This certainly won't work for complex time specifications (e.g. */2) and only reports on the first part of the command. All of this can be corrected, but probably you'd be better off doing it in perl or something.
由于如果不解析 cron 这似乎是不可能的,所以我决定自己用 perl 编写它:
(不知道为什么格式是fubar)
Since this doesn't seem possible without parsing cron, I decided to write it myself in perl:
(not sure why the formatting is fubar)
您可以将 crontab 文件复制并粘贴到 Excel 中,然后创建一些函数来执行此操作。由于时间字段始终位于同一位置,因此 Excel 中的列将对应于执行该命令的时间范围。
您必须编写公式,以便考虑单个整数值和重复值(例如 10 与 */10)。
如果您的 crobtab 文件更改很多并且有很多条目,那么您可能可以编写一个 php 脚本来快速解析此信息。
You could copy and paste the crontab file into Excel and then create a few functions that do this. Since the time fields are always in the same place, your columns in Excel would correspond to the timeframe in which that command is executed.
You'll have to write your formula so that is accounts for single integer values and repeating values (like 10 versus */10).
If your crobtab file changes a lot and you have many entries, then you could probably write a php script to parse this information pretty quickly.
您可以使用 Ruby 1.8.7 gem "crontab-parser" 来解析 crontab,然后迭代两个时间戳之间的每一分钟,在每个 crontab 条目上调用
should_run?
:因此,如果您有一个如下所示的
crontab.txt
:然后使用以下命令调用脚本来自
date
或类似文件以及包含 crontab 的文件的输出:您将得到如下输出:
You can use the Ruby 1.8.7 gem "crontab-parser" to parse a crontab, and then iterate through each minute between the two timestamps, calling
should_run?
on every crontab entry:So, if you have a
crontab.txt
that looks like this:Then call the script with output from
date
or similar and a file containing a crontab:You'll get output like this: