什么是执行中的Bash时间戳比较的最佳途径 [英] What is the Best Way to Perform Timestamp Comparison in Bash
问题描述
我有我试图从垃圾邮件我留着,所以我想打一个条件,如果警报已内发送,说最后一个小时的警报脚本,不发送另一个警报。现在我有一个检查条件的每一分钟,因为我需要在条件满足时迅速惊动但我并不需要得到每一个邮件,直到munite我得到控制的问题cron作业。什么是bash中进行比较的时间做到这一点的最佳方式?
I have an alert script that I am trying to keep from spamming me so I'd like to place a condition that if an alert has been sent within, say the last hour, to not send another alert. Now I have a cron job that checks the condition every minute because I need to be alerted quickly when the condition is met but I don't need to get the email every munite until I get the issue under control. What is the best way to compare time in bash to accomplish this?
推荐答案
目前最简单的是存储时间标记为空文件修改时间。 GNU 触摸
和日期
命令可以设置/获取这些时间和执行日期计算。 Bash有测试,以检查文件是否比小于( -ot
)的另一个新的( -nt
)或以上。
By far the easiest is to store time stamps as modification times of dummy files. GNU touch
and date
commands can set/get these times and perform date calculations. Bash has tests to check whether a file is newer than (-nt
) or older than (-ot
) another.
例如,只发送一个通知,如果最后通知是一个多小时前更多:
For example, to only send a notification if the last notification was more than an hour ago:
touch -d '-1 hour' limit
if [ limit -nt last_notification ]; then
#send notification...
touch last_notification
fi
这篇关于什么是执行中的Bash时间戳比较的最佳途径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!