Mail when no mail

Sometimes you want to get a warning if you don't get a mail. E.g. a mail about a backup.

Let's say the machine sigmund.vandervlis.nl send ones a day a message to the machine control.vandervlis.nl. In this example I will use a simple cronjob for it on the sigmund-machine in /etc/crontab:

# mailcheck
00 8    * * *   nobody echo "hi" | mail -s "test" sigmund@control.vandervlis.nl

As you see, I use a special mailadres and the machinename.

I will create the mailadres on the control-machine in /etc/aliases this way:

sigmund: /var/spool/mailcheck/sigmund, backupmailbox@vandervlis.nl

Don't forget to run "newaliases" after editting the file.

What I do here is saving the mail as a file (see "man aliases" for more information), and send it to some mailbox. Realize that you cannot do this on virtual maildomains so far I know.

Then I edit /etc/crontab on the control-machine:

# mailcheck
10 8 * * * nobody /usr/local/bin/mailcheck

I use a directory /var/spool/mailcheck, I will create it here. I use postfix, what uses the user "nobody", maybe this is another user when you use Exim of Sentmail (or you can do it as root, what's more easy).

Now I create a spool-directory:

mkdir /var/spool/mailcheck
chown nobody /var/spool/mailcheck

Then, as the last part, we have to make the script /usr/local/bin/mailcheck :

# send mail when no mail arrives

list="sigmund"

for server in $list; do
  if [ ! -f "/var/spool/mailcheck/$server" ]; then
    echo "hi" | mail -s "No mail from $server" paul@vandervlis.nl
  else
    rm /var/spool/mailcheck/$server
  fi
done

Don't forget to make the script executable.

When it's good, you will get now a message at 8:10 every morning, when there was no mail from the server. As you can see, you can simply add more machines to the list. If you don't need the mail, because it's only a testmail, you don't have to use the ", backupmailbox@vandervlis.nl"-part in the crontab.

If you liked this page, you could do something like this:

echo "thanks for the MailWhenNoMail page, it helped me" | mail -s "thanks" paul@vandervlis.nl