Skip to navigation

Shell script to periodically check mail logs (and msmtpQ queue) and notify user by mail

As said in this previous post, I generally really like the speed of having

set sendmail_wait = -1

in my ~/.muttrc, but mutt doesn’t let me know if there are any error messages. That’s not good, and it would be ironic to gain a few seconds when hitting y in mutt just to nervously dig around in error logs all the time. In addition, while I like my new mail queue setup for the laptop,I find that, too, a bit too silent: the mail gets queued without any notification whenever I’m offline, and queued mail doesn’t get sent automatically when I’m back online (I actually prefer that — better that than inadvertently sending off mails that I was planning to delete from the queue).

So I wrote a shell script that checks both for errors and for queued mail and notifies me via the local exim4 MTA with messages that go into /var/mail/username. I’ve put that shell script online: checklog. This is all pretty basic stuff, but it might come in handy for someone, or if you’re new to shell scripting, you can never have too many examples.

I’ve put it into my ~/bin, made it executable, and call it every two hours and at reboot from ~/.crontab.

0 */2 * * *  /home/mpromber/bin/checklog
@reboot      /home/mpromber/bin/checklog

To make it take effect:

crontab ~/.crontab

On the laptop, I’ve also put a symbolic link to that script into /etc/acpi/resume.d so it gets executed on waking up from suspend, because that’s likely to have a correlation with wanting to be reminded of queued mail:

sudo ln -s /home/mpromber/bin/checklog /etc/acpi/resume.d/99-checklog.sh

Note that I had to give the symlink a name ending in “.sh”, since /etc/acpi/resume.sh above that directory only calls scripts with that pattern:

#!/bin/bash

# Source from /etc/acpi/resume.d/
for SCRIPT in /etc/acpi/resume.d/*.sh; do
    if [ -x $SCRIPT ]; then
        . $SCRIPT
    fi
done

Comments are closed.