在Bash中执行时间戳比较的最佳方法是什么?(What is the Best Way to Perform Timestamp Comparison in Bash)

编程入门 行业动态 更新时间:2024-10-17 22:20:34
在Bash中执行时间戳比较的最佳方法是什么?(What is the Best Way to Perform Timestamp Comparison in Bash)

我有一个警告脚本,我试图阻止垃圾邮件发送给我,所以我想提出一个条件,如果在最后一小时内发送了警报,不发送另一个警报。 现在我有一个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 touch和date命令可以设置/获取这些时间并执行日期计算。 Bash有测试来检查一个文件是否比( -nt )更新或比( -ot )更新。

例如,如果最后一次通知超过一小时前才发送通知:

touch -d '-1 hour' limit if [ limit -nt last_notification ]; then #send notification... touch last_notification fi

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

更多推荐

本文发布于:2023-07-14 14:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1105089.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:执行时间   方法   Bash   Comparison   Timestamp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!