Operating System - HP-UX
1834873 Members
2645 Online
110070 Solutions
New Discussion

I need some help w/automating a script in cron and emailing the output -

 
SOLVED
Go to solution
Rob Johnson_3
Regular Advisor

I need some help w/automating a script in cron and emailing the output -

I have 5 scripts I need to setup in cron to have run 8 times each day and email the the output which is about 5 pages of text. I would like to have the script name and the date and time for each execution somewhere in the email.

This is one script:
johnsonr@nmscrme01 > cat emdsils1.sh
#!/bin/sh
for device in `cat /opt/home/johnsonr/scripts/perl/emdsils1-node`
do
/opt/home/johnsonr/scripts/perl/emdsils1 $device
done
johnsonr@nmscrme01 >

The other scripts are named emdsils2.sh, emdsils3.sh, egaglks1.sh, egaatls1.sh.

This would be the crontab entry for one script.
00 6,8,10,12,14,16,18,23 * * * /opt/home/johnsonr/scripts/perl/emdsils1.sh

However, I don’t know how to have the contents of the output emailed. I’m guessing the output from each script would need to be written to a file and then emailed out.

Also, I don’t know if the output should be written to the file somewhere in the .sh scripts that basically read a perl script and also get hostnames from the emdsils1-node file.

Ideas on how I could accomplish this?
6 REPLIES 6
Jeff_Traigle
Honored Contributor
Solution

Re: I need some help w/automating a script in cron and emailing the output -

Various ways to accomplish this. If your script outputs everything to stdout, then cron will email it to the user the cron runs under automatically. You could also use a pipe in the cron command to mailx to mail the output to some other email addresses. You could also send the output to a file with the script as you mentioned and put a mailx command in there to send it.
--
Jeff Traigle
jamesps
Regular Advisor

Re: I need some help w/automating a script in cron and emailing the output -

Here's how you can send e-mails in a few different ways with sendmail, elm and mailx:

echo "Message" | sendmail -v user@email

echo "Message" | mailx -s 'subject' user@email

echo "Message" | elm -s 'subject' user@email

echo `date` | mailx -s 'subject' user@email

(echo "Subject: e-mail subject here" ; echo "test body message") | sendmail user@email

Hope this helps!
james
James R. Ferguson
Acclaimed Contributor

Re: I need some help w/automating a script in cron and emailing the output -

Hi Rob:

With crontasks, any un-redirected STDOUT or STDERR is automatically mailed to the user.

In that respect you may not need to do anything.

Otherwise you can send mail to one or more users like this:

# mailx -s "You have mail!" root < yourfile

If you want to add Rob to the mail above, you could do:

# mailx -s "You have mail!" root,rob < yourfile

If you are creating temporary files in your scripts, its a good idea to include the process id (pid) as part of the filename. Use the "$$" shell variable to do that:

# echo "My notes..." > /tmp/mysh.$$

As for labeling your mail with the script name and date of the run, do something like:

# echo "I am $0 run at `date '+%D %T'`"

The shell variable $0 gives your process name.

Regards!

...JRF...
Rob Johnson_3
Regular Advisor

Re: I need some help w/automating a script in cron and emailing the output -

Thanks for the input. I'll start working on it and will most likely have more questions coming up.

Rob Johnson_3
Regular Advisor

Re: I need some help w/automating a script in cron and emailing the output -

I got it working with each script. I used the subject field and put the script name here. The time stamp can be read from when the message was delivered. This will work just fine. It sure made it nice and easy that the output was mailed automajically.

Thanks for the help.
Yogeeraj_1
Honored Contributor

Re: I need some help w/automating a script in cron and emailing the output -

hi Rob,

this is what i usually put when i have to send an attachment from a script (cron)

/usr/bin/uuencode $LOG_PATH/process"$dt".log "process.log"|mailx -m -s "Mail header for - `date`" $EMAILADD


where $LOG_PATH and $EMAILADD are environment variables.


hope this helps too!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)