- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Script failing to send body of the e-mail (som...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-27-2004 10:54 AM - last edited on тАО01-30-2012 01:16 AM by Ajay-Kumar
тАО10-27-2004 10:54 AM - last edited on тАО01-30-2012 01:16 AM by Ajay-Kumar
I've written a general function that will send out alert e-mail's to admins. Sometimes it includes the body of the e-mail and sometimes it doesn't, and I can't figure out why it's inconsistant.
If I run a script manually that calls this function using my user id, everything works perfectly.
If I run a script via cron using my user id, everything works perfectly.
If I run a script manually as root, everything works perfectly.
If I run a script via root's cron, everything works perfectly.
If the script is kicked off by the backup process using root's id, it only sometimes works!!!
The problem is that even though I pass $4 into the function and it's written to $MAILFILE (I've verified that it is being written!!!), when the e-mails are delivered, they are sometimes missing the message itself. Only the header portion arrives....
Also, if I do NOT pass in a message, the default message, "No text message available." is included and delivered correctly in e-mails that get sent out!
I've tried pretty much everything I can think of. Any ideas???
# send_email_alert ()
# This function delivers all e-mail alerts via sendmail, attaching the
# appropriate headers.
#
# It accepts four parameters:
# $1 : The addressee(s) (To:)
# $2 : The subject (Subject:)
# $3 : The priority (Importance: [Normal|High])
# $4 : The text body, if any
#
# When calling send_email double-quotes (e.g. "$1") should be used
# around all fields.
#
send_email_alert ()
{
MAILFILE=$(/bin/mktemp)
# Build the header...
/bin/echo "To: $1" > ${MAILFILE}
/bin/echo "Subject: $2" >> ${MAILFILE}
/bin/echo "Importance: $3" >> ${MAILFILE}
# if no text body was passed, need to default the value...
if [ "X${4}" = "X" ]
then
MSGBODY="No text message available."
else
MSGBODY="$4"
fi
/bin/echo "${MSGBODY}" >> ${MAILFILE}
# Send the e-mail
/bin/cat ${MAILFILE} | /usr/sbin/sendmail -t
# cleanup....
# /bin/rm ${MAILFILE}
return
}
-------------------------------
P.S. This Thread has been moved from HP-UX --> Languages & Scripting to HP-UX --> Messaging - Forum Moderator
Solved! Go to Solution.
- Tags:
- sendmail
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-27-2004 11:50 AM
тАО10-27-2004 11:50 AM
Re: Script failing to send body of the e-mail (sometimes....)
Last night, this is one of the e-mail message files that was sent out missing the body. As you can see the temp file, a18833, contains the message "bpstart_notify.niobe_db_erhr: started: Tue 10/26/04 21:06", but the e-mail that was delivered did not!
$ cat a18833
To: dba
Subject: PSMDB-I: Backup Initiated (prod11i)
Importance: Normal
bpstart_notify.niobe_db_erhr: started: Tue 10/26/04 21:06
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-27-2004 11:54 AM
тАО10-27-2004 11:54 AM
Re: Script failing to send body of the e-mail (sometimes....)
But you could try onething. I am little skeptical about catting and piping to sendmail. Let the sendmail read the file directly.
/usr/sbin/sendmail -t < ${MAILFILE}
Try this if you would like and see if it helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-27-2004 12:01 PM
тАО10-27-2004 12:01 PM
Re: Script failing to send body of the e-mail (sometimes....)
Since you've verified that the $MAILFILE is getting the body written correctly to it, I would suspect something happening inside of sendmail. Have you noticed any difference in the body part of the MAILFILE for ones that didn't succeed? I wonder if it needs a blank line after the body to make it work?
One thing I have done for testing variables is to use the '-z' test to see if the variable has a zero length. Your test for the $4 parameter would look something like this:
if [[ -z $4 ]]
then
MSGBODY="No text message available."
...
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-27-2004 12:10 PM
тАО10-27-2004 12:10 PM
Re: Script failing to send body of the e-mail (sometimes....)
:-). But since Jim has mentioned that the temp file is actually being written to, any problem with the if [ "X${4}" is ruled out.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-27-2004 12:20 PM
тАО10-27-2004 12:20 PM
Re: Script failing to send body of the e-mail (sometimes....)
I understand. I just added that as an afterthought about testing the variable. Our minds running along the same line? That's scary. My mind usually runs in circles. :)
Another idea I just had is that you could try and catch the return code from sendmail to see if it is complaining about anything. It might not help much but it might be worth looking at.
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-27-2004 06:51 PM
тАО10-27-2004 06:51 PM
Solution2. Be sure there is no line in the body tht consists of a single dot, because it will end the body
I'd personally would do it with Perl :)
--8<---
#!/opt/perl/bin/perl
use strict;
use warnings;
@ARGV >= 3 or die "usage: $^0
use Mail::Sendmail;
my ($to, $sbj, $imp, @bdy) = @ARGV;
if (@bdy == 1 && -f $bdy[0]) {
$/ = undef;
@ARGV = (@bdy);
@bdy = <>;
}
my $bdy = join " " => @bdy;
$bdy =~ m/\S/ or $bdy = "No Text message available.";
sendmail (To => $to, Subject => $sbj, Importance => $imp, Message => $bdy);
-->8---
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-28-2004 06:07 AM
тАО10-28-2004 06:07 AM
Re: Script failing to send body of the e-mail (sometimes....)
Tried: /usr/sbin/sendmail -t < ${MAILFILE}
No change in behaviour.
John,
The return code of sendmail itself is always 0, as an e-mail is being sent successfully, it's just that the body is sometimes missing.
procura,
I'll try adding the initial blank line for tonight's run.
fi
/bin/echo "" >> ${MAILFILE}
/bin/echo "${MSGBODY}" >> ${MAILFILE}
/usr/sbin/sendmail -t < ${MAILFILE}
If I can't figure out why this is happening, I just may "borrow" your perl script...
I've already tried adding "." as the last line in the message, and that fails.
I've tried adding 2, 5 and 10 second sleeps between the last write to $MAILFILE and the invocation of sendmail, and that fails too.
...maybe the following is a clue, maybe not...these two e-mails were generated for delivery last night. The first got sent out missing the body. The second was sent with the body included. The only difference between the two function calls is their location in the script. The first call is roughly half-way through, the second call is the 2nd last statement of the script, and the last time that the function is called.
[tank|jdonovan]
$ cat a00827
To: dba
Subject: PSMDB-I: Backup Completed (prod11i)
Importance: Normal
Testing: e-mail alert. At this point bcvs are unmounted
[tank|jdonovan]
$ cat a01113
To: dba
Subject: PSMDB-I: Backup Completed (prod11i)
Importance: Normal
bpend_notify.niobe_db_erhr ended: Thu 10/28/04 02:21
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-28-2004 06:38 AM
тАО10-28-2004 06:38 AM
Re: Script failing to send body of the e-mail (sometimes....)
bin/echo "${MSGBODY}" >> ${MAILFILE}
like this:
bin/echo "/tmp/[filename]" >> ${MAILFILE}
and see if that changes your results any.
mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-28-2004 07:36 AM
тАО10-28-2004 07:36 AM
Re: Script failing to send body of the e-mail (sometimes....)
Perhpas try and trap a return code from critical steps.
rc=$?
if [ $rc -ne 0 ]
then
echo "oops Jim, something went wrong: $rc"
fi
Add to the echo statement so you know what line of code blew up.
I'm attaching a script, based no something I stole from an itrc document. It doesn't do that but I would not try my code as a first resort.
Regards,
Steve
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com