[TriLUG] send an email within a script

Jeremy Portzer jeremyp at pobox.com
Fri Mar 5 09:32:01 EST 2004


On Fri, 2004-03-05 at 08:47, Douglas Kojetin wrote:
> I run some computational jobs that take (sometimes) a long time -- they 
> basically consist of a bunch of piped lines into various programs 
> within a C-shell script.  Anywho, the programs I use have the option of 
> displaying progress (verbose) or not.  I'd like to turn off the 
> verboseness ... but would like to have some sort of notification that 
> the job has finished (other than using 'top' to check and see if the 
> processes are still running).
> 

Normally you would use the /bin/mail program.  This relies on a
sendmail-compatible MTA being set up on the system, perhaps with a
smarthost set to a departmental relay server.  (Sendmail compatible
could easily be postfix or exim or qmail or whatever.)

For example,

echo "Job finished at `/bin/date`" | 
	/bin/mail djkojeti=at=unity.ncsu.edu -s "Job finished"

You don't really need the `/bin/date` in there since the mail itself
will show that in the headers, but whatever.

If you don't have an MTA installed on the system (or it's not properly
configured), /bin/mail won't work.  One option is the Mail::Sendmail
Perl module which will send mail with a specified remote SMTP server.

If you do have a local MTA but need more flexibility over what /bin/mail
(sometimes called mailx) can do for you, you can give messages straight
to /usr/sbin/sendmail (the wrapper for mail from standard input -- this
binary is called this regardless of which brand of MTA you have
installed).  Here's an example:

==cut here==
#!/bin/bash

TMPFILE=`mktemp -t mail.XXXXXXX`

cat > $TMPFILE <<EOF
Date: `date -R`
Subject: Job Completed at `date`
From:  Job Completion Script <valid.return.address at example.tld>
To: Douglas Kojetin <djkojeti=at=unity.ncsu.edu>

This is the body of the message, saying that the job
completed at: `date`

Thank you,
Job Completion Script
EOF

/usr/sbin/sendmail -t < $TMPFILE
/bin/rm -f $TMPFILE

===cut here===

You could also with something like this:
	/usr/bin/sendmail -t << EOF
But I like demonstrating proper use of temp files with mktemp, because
you may need them in other situations.

Hope this helps,

Jeremy

-- 
/---------------------------------------------------------------------\
| Jeremy Portzer        jeremyp at pobox.com      trilug.org/~jeremy     |
| GPG Fingerprint: 712D 77C7 AB2D 2130 989F  E135 6F9F F7BC CC1A 7B92 |
\---------------------------------------------------------------------/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://www.trilug.org/pipermail/trilug/attachments/20040305/a3cdbe12/attachment.pgp>


More information about the TriLUG mailing list