Search code examples
bashunixmailx

How to check if Mailx used in the past timeframe?


I have a mailx statement as part of a shells scipt. It is part of a condtional statement that will send an email every minute if a system is failing.

Is there a way to have mailx check if a mail was send in the last hour and only send if result is false?

tail -1 "/location/of/file.txt" | mail -s "Warning" test@testing.com;

Solution

  • use a file to mark the time you send. e.g.

    dowork() {
        tail -1 "/location/of/file.txt" | mail -s "Warning" test@testing.com;
        touch ./checked.txt
    }
    if [[ -f ./checked.txt ]] ; then
        if [[ $(expr $(date '+%s') - $(stat -c '%Y' ./checked.txt )) -gt 3600 ]]; then
                dowork
        fi
    else
        dowork
    fi