In my latest postings, I have been documenting the failure of anacron and/or Back In Time to run a daily backup of my data partition. My problem with the relevant cron job failing “silently” was that I wasn’t even sure that the cron job ran in the first place. So, I started looking for a method by which I could receive notification that anacron had indeed launched the scripts in the folder cron.daily. In turn, this would indicate that my backup script had tried to run but had failed in some manner.
One thought that came to mind was to include a second script in the cron.daily file that would send me an E-mail message. Then, if I received this confirmation message, I would at least be assured that the script files were being launched by anacron. But, how does one send an E-mail from a bash script file?
It’s actually rather simple. First, install the mailutils package from the Ubuntu Software Centre. Then, create the equivalent (using your user and computer names) of the following bash script file:
# Confirm cron.daily jobs are running by sending a mail message to toaster
echo “Cron.daily jobs have run” | mail -s Confirmation toaster@Dell-XPS8300
The last line is the mail command. Note that it pipes a text string (“Cron.daily jobs have run”) from the echo command to the mail program. The -s switch on the mail command indicates that the subject of the message is to be “Confirmation”, while the final parameter provides the address to which the message is to be sent. In this case, the message is aimed at my user account (toaster) on the local machine (Dell-XPS8300). However, note that any E-mail address could be used here, so the message could just as easily be sent to the E-mail account on my ISP’s mail server.
Running the mail utility, indicates that the message has been received:
“/var/mail/toaster”: 1 message 1 new
>N 1 Alan Mon Sep 10 10:22 13/466 Confirmation
The message can be displayed, simply by entering “1” in response to the “?” prompt:
Received: by Dell-XPS8300 (Postfix, from userid 1000)
id AB1394DC59; Mon, 10 Sep 2012 10:22:21 -0400 (EDT)
X-Mailer: mail (GNU Mailutils 2.1)
Date: Mon, 10 Sep 2012 10:22:21 -0400 (EDT)
From: toaster@Dell-XPS8300 (Alan)
Cron.daily jobs have run
To delete this message and exit from the mail program, enter the following commands at the prompts:
Sometimes, testing a system generates lots of messages so, rather than deleting them one by one, to remove multiple messages, use the delete command in the form d1-12. Alternatively, if you wish to retain the available messages, simply enter quit and you will be able to re-read your mail next time you run the utility.
Having placed this script file in /etc/cron.daily and having received the confirmation E-mail, I am now assured that the anacron is indeed running the daily scripts. Now, it’s time to progress a little further and try to determine precisely what is happening with the backup script.
Send mail to root when crontab command fails to run
/etc/cron.daily/foo : Send email to a particular user instead of root?