Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

send email automatically via mail command

Status
Not open for further replies.

flypoo

MIS
Mar 22, 2005
44
AU
I just need to be steered in the right direction. I have had no luck with the mail command.. sending email notification in a script without any user interaction.

I am backing up my mail server using tar, then transferring via ftp to another server which backs it up to tape. Now the icing on the cake is the email notification sent to me to ensure that it worked.

i am thinking about logging the scripts progress then emailing that as an attachment. I persume this is possible yeah?

For now I would just be happy to receive an email confirmation that the script was executed.

Thanks
 
Something like...
Code:
export NAME=flypoo
export RC=<capture return code from backup here>
export LOG=/path/to/logfile.log
export MAILTO=someone@somewhere.com

( cat <<-MSG
Hi ${NAME},

The backup has completed $(date)
with a return code ${RC}. See attached log
for details.

Signed,
$(grep "^`whoami`:" /etc/passwd | cut -d: -f5)
MSG
uuencode ${LOG} $(basename ${LOG}
) | mail -s "Backup log as of $(date)" ${MAILTO}
That will send a nice little message with the log attached. You can change to suit your tastes.

Hope this helps.
 
What does 'unexpected end of line' mean?

That is an error I am getting.
 
Oops! My bad! Just add a closing parenthesis at the end of the [tt]uuencode[/tt] line.

It should be...
Code:
export NAME=flypoo
export RC=<capture return code from backup here>
export LOG=/path/to/logfile.log
export MAILTO=someone@somewhere.com

( cat <<-MSG
Hi ${NAME},

The backup has completed $(date)
with a return code ${RC}. See attached log
for details.

Signed,
$(grep "^`whoami`:" /etc/passwd | cut -d: -f5)
MSG
uuencode ${LOG} $(basename ${LOG})
) | mail -s "Backup log as of $(date)" ${MAILTO}
That should do it.

Hope this helps.
 
As I was copeing the code, I saw my mistake... I didn't have MSG after the text.

That worked a treat. Thanks heaps.
 
I haven't tried attaching the log yet but the email was sent, I will let you know.
 
SamBones,

when you say <capture return code here>, do you mean just the error code (echo $?)? or is there a better way of doing it?

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top