MailMe: Simple Bash to Notify Your Command Status via Email

I usually having problem whereby I always forgot to check what happen to my copying or downloading progress in the server. This has gives me idea to create a script to notify me via email once the command executed and completed.

For example, I usually download a big installer file which usually make me constantly check the download progress. I just need an alert to be send to me once the respective command completed whether it is failed or succeed. Another case is when I am running a big migration. I need to copy the whole /home directory to external hard disk in this will takes days to complete. Using MailMe will definitely increase my works efficiency. I just need to run the respective command and wait for the notification email. Thats all.

1. Install sendmail and mailx using yum. Mailx is required. You can use Postfix or any other SMTP server to send the email instead of sendmail:

$ yum install sendmail mailx -y

2. Start the sendmail service:

$ chkconfig sendmail on
$ service sendmail start

3. Download and integrate the script into environment. We will need to place the script under /usr/local/bin directory.

$ wget http://blog.secaserver.com/files/mailme -P /usr/local/bin
$ chmod 755 /usr/local/bin/mailme

4. We need to change the MAILTO value so the script will send the notification automatically to your email. Open the script using text editor:

$ vim /usr/local/bin/mailme

And change following line:

Done. Now you can integrate mailme into your command. Example as below:

– Download the CentOS 6.3 64bit ISO:

$ mailme 'wget http://centos.ipserverone.com/centos/6.3/isos/x86_64/CentOS-6.3-x86_64-bin-DVD1.iso'

– Rsync the whole backup directory to another server:

$ mailme 'rsync -avzP /backup/*.tar.gz [email protected]:/backup'

Once the command executed successfully, you will get simple email notification like below:

Subject: MailMe Command Notification: test.servering.com
Status: SUCCESSFULLY COMPLETED
Command: wget http://wordpress.org/latest.tar.gz
Date/Time: Mon Oct 1 11:14:54 MYT 2012

3 thoughts on “MailMe: Simple Bash to Notify Your Command Status via Email

  1. Hello,

    Your post is very helpful.
    I run also dos2unix /usr/local/bin/mailme

    How can I get also output of commands?

    Thank you!

    Reply

    1. Hi Lucian, I am sorry that I miss out this. This script will only report the status of command either succeed or failed.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *