If you ever used a commercial cloud computing service, you might know their nice monitoring services, which inform you when jobs are done.

If you run long running programs, scripts, or services on your own machine, you don’t want to have to look at them every few hours, days, or weeks to see if their done. I set up my server to send me an e-mail when certain jobs are have finished.

First you have to set up an e-mail service on your machine. I use ubuntu, but i’ve seen similar how-tos for other distributions.

We need to install ssmtp.

apt-get install ssmtp

Then we need to configure the service, I use the Gmail settings here:

nano /etc/ssmtp/ssmtp.conf

and add/change the following lines:

AuthUser=[user-name]
AuthPass=[password]
mailhub=smtp.gmail.com:587
UseSTARTTLS=YES

After that, you should be able to use mail or mutt. We will set up a command in $HOME/.bashrc (just add it to the end of the file):

alias alert='echo "[your-message-goes-here]" | mutt -s "[$?] $(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/;\s*alert$//'\'')" [your-email-goes-here]'

The code

"[$?] $(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/;\s*alert$//'\'')"

contains the subject, i.e. [$?] gives the exit code (if its 0, everything went fine) and it takes the name of the last command you run before starting alert.
If we run alert, an e-mail will be send with our message, the last command as subject to the email-address given above.

The next time we run a script, we just add a line bash -ic alert to the end of the script. If your using a shell script it won’t be able to find aliases defined in your .bashrc unless you use this command. If your not using a shell script, just add
; alert
to end of your command. When the script finishes, we get an email telling us. Done.

p.s.: my .bashrc already contained an alert command (it sends desktop-notifications). Just rename it if you want.

Sources:

http://www.simplehelp.net/2008/12/01/how-to-send-email-from-the-linux-command-line/

http://www.cyberciti.biz/tips/linux-use-gmail-as-a-smarthost.html

http://linuxundich.de/ubuntu/benachrichtigungen-beim-beenden-langwieriger-prozesse/