Operating System - HP-UX
1833465 Members
2463 Online
110052 Solutions
New Discussion

Scripted report via email

 
SOLVED
Go to solution
Doug Wheeler_1
Frequent Advisor

Scripted report via email

I want to send a text file via email from a script. At present, this is what I do :

cd /home/reports
touch bdf.txt
date > bdf.txt
bdf >> bdf.txt
elm -s "bdf Report" root < bdf.txt
rm bdf.txt

This works fine until I try to run it via cron., as it returns the status lines:
sending mail
mail sent!

When it does this, cron doesn't like it and generates an error.

So... how do I get around this? Is there a script command to read the responses or is there a better way?

Many Thanks,
Doug
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: Scripted report via email

Hi Doug:

'elm' is by design, interactive. 'cron' doesn't "like" interactive scripts! Try this, instead:

# mailx -s "bdf Report" root < bdf.txt

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Scripted report via email

Actually that should work but there must be a ${HOME}/.elm directory owned by the cron user in the cron user's home direcory.

Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Scripted report via email

You also should have a Mail directory in the cron users home directory.
If it ain't broke, I can fix that.
Jason Morgan_1
Advisor

Re: Scripted report via email

Doug,

I have a script that does a bdf and some other things that I like to keep track of on a daily basic. Below is the script and my cron entry.
=====
dailyfile=/home/root/dailychanges
echo "Daily Changes / `uname -n` / `date`" > $dailyfile
echo "" >> $dailyfile
echo "BDF" >> $dailyfile
bdf |grep vg00|awk '{print $5 " " $6}' >> $dailyfile
echo "" >> $dailyfile
echo "" >> $dailyfile
netstat -rn >> $dailyfile
echo "" >> $dailyfile
echo "" >> $dailyfile
echo "Netconf" >> $dailyfile
cat /etc/rc.config.d/netconf |grep -i IP_ADDRESS |grep -v '#' >> $dailyfile
cat /etc/rc.config.d/netconf |grep -i INTERFACE_NAME |grep -v '#' >> $dailyfile
echo "" >> $dailyfile
echo "" >> $dailyfile
echo "Uptime" >> $dailyfile
uptime >> $dailyfile
cat $dailyfile | mailx -s "Daily Changes Report for `uname -n`" YOU@YOURDOMAIN.com
=====

Here is my entry in cron.
=====
# script checks for daily changes
0 20 * * * sh /home/root/daily_changes.sh
=====

-Jason
Never Underestimate the Power of Human Stupidity -RAH
Doug Wheeler_1
Frequent Advisor

Re: Scripted report via email

Thanks all for your help. I have tested and implemented the
'mailx -s "bdf Report" root < bdf.txt '
command as suggested by James.
Also thanks to Clay & Jason for your help as well.
Doug



James R. Ferguson
Acclaimed Contributor

Re: Scripted report via email

Hi Doug (again):

OK, a better answer to your question:

I assume that you are being bothered by the additional mailing (by cron) announcing "Cron: The previous message is the standard output
and standard error of one of your crontab commands" when you use 'elm'.

You can circumvent this in one of several ways:

1. Use 'mailx' as I first suggested, since it doesn't write "mail sent!" and cause 'cron' to "complain".

2. Use 'elm' exactly as you have been doing, but construct your cron entry along this line:

# * * * * * /thescript > dev/null 2>&1

or, make your script's statement like this:

# elm -s "bdf Report" root < bdf.txt > /dev/null 2>&1

...JRF...