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?
相关问题
- JQ: Select when attribute value exists in a bash a
- How to account for clock offsets in a distributed
- bash print whole line after splitting line with if
- “command not found” errors in expect script execut
- grep using grep results
相关文章
- Check if directory exists on remote machine with s
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
- Bash script that creates a directory structure
- Test if File/Dir exists over SSH/Sudo in Python/Ba
- How can I create a “tmp” directory with Elastic Be
- Get POSIX epoch as system_clock::time_point
By far the easiest is to store time stamps as modification times of dummy files. GNU
touch
anddate
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:
Use "test":
EDIT: If you want to know when an event occurred, you can use "touch" to create a file which you can later compare using "test".
Use the date command to convert the two times into a standard format, and subtract them. You'll probably want to store the previous execution time in a dotfile then do something like:
(Thanks, Steve.)