LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   cron job email results... (https://www.linuxquestions.org/questions/linux-general-1/cron-job-email-results-282601/)

ryedunn 01-26-2005 04:46 PM

cron job email results...
 
I have a cron job /etc/cron.daily/update and I would like to be email the results. The problem is there are multiple commands within the job. For example:
Code:

#!/bin/sh

LOG=/var/log/updates
echo "Running updates on `date`" > $LOG
urpmi.update updates >> $LOG 2>&1
urpmi --media updates --auto-select --auto >> $LOG 2>&1

This makes my logs nice a pretty and I know I could get this to send me multiple emails, but Im curious if I could get everything (for that day) to be sent to me in 1 email.

Code:

Running updates on Wed Jan 26 13:05:46 CST 2005
examining synthesis file [/var/lib/urpmi/synthesis.hdlist.jpackage.cz]
examining synthesis file [/var/lib/urpmi/synthesis.hdlist.plf.cz]
examining synthesis file [/var/lib/urpmi/synthesis.hdlist.contrib.cz]
retrieving source hdlist (or synthesis) of "updates"...
    ftp://mirror.aca.oakland.edu/pub/lin...info/hdlist.cz
...retrieving done                                                           
examining hdlist file [/var/cache/urpmi/partial/hdlist.updates.cz]
writing list file for medium "updates"
examining synthesis file [/var/lib/urpmi/synthesis.hdlist.main.cz]
performing second pass to compute dependencies

examining hdlist file [/var/lib/urpmi/hdlist.jpackage.cz]
built hdlist synthesis file for medium "jpackage"
examining hdlist file [/var/lib/urpmi/hdlist.plf.cz]
built hdlist synthesis file for medium "plf"
examining hdlist file [/var/lib/urpmi/hdlist.contrib.cz]
built hdlist synthesis file for medium "contrib"
examining hdlist file [/var/lib/urpmi/hdlist.updates.cz]
built hdlist synthesis file for medium "updates"
examining hdlist file [/var/lib/urpmi/hdlist.main.cz]
built hdlist synthesis file for medium "main"
found 0 headers in cache
removing 0 obsolete headers in cache
write config file [/etc/urpmi/urpmi.cfg]

    ftp://mirror.aca.oakland.edu/pub/lin...01mdk.i586.rpm
    ftp://mirror.aca.oakland.edu/pub/lin...01mdk.i586.rpm
    ftp://mirror.aca.oakland.edu/pub/lin...24mdk.i586.rpm
installing /var/cache/urpmi/rpms/xpdf-3.00-7.3.101mdk.i586.rpm /var/cache/urpmi/rpms/libcups2-1.1.21-0.rc1.7.4.101mdk.i586.rpm /var/cache/urpmi/rpms/kernel-source-2.6-2.6.8.1-24mdk.i586.rpm
Preparing...                ##################################################
  1:kernel-source-2.6      ##################################################
  2:xpdf                  ##################################################
  3:libcups2              ##################################################

Since the job appends to the log, I dont know how to receive only 1 email with todays info, not the whole log.

Thank you!
TheGNUbie

deoren 01-26-2005 05:08 PM

At the end of the cron job, do something like this:

Code:

cat $LOG | grep `date` | mail -s "The log from today" your.email@example.com

deoren 01-26-2005 05:09 PM

Where you have `date`, use anything you want to match on.

ryedunn 01-26-2005 05:35 PM

yeah but
 
wont that only return 1 line? (ie.. the date line)

I want to see everything from todays date down.

ryedunn 01-26-2005 05:44 PM

actually I just notice that its not appending to the log like I had hoped.. so I guess I can just email the whole log to myself.

deoren 01-26-2005 09:03 PM

Quote:

echo "Running updates on `date`" > $LOG
That echoes the contents of `date` to the file. You've pointed out that this overwrites the file. If you didn't want to overwrite the file, you would have used >>

Okay.

Let's expand what I mentioned before.

Quote:

cat $LOG | grep `date` | mail -s "The log from today" your.email@example.com
That expands to:

(We'll use a fake value for $LOG)

cat /var/log/fake.log | grep Wed Jan 26 21:54:32 EST 2005 | mail -s "The log from today" your.email@example.com

Which won't work. I didn't think through my example clearly enough. You'll need to format the date differently.


Something like this:
Quote:

`date '+%m%d%y'`
would produce this: 012605. That might be good enough. If you wanted to use the same format used by apps that print to /var/log/messages (on a FC1 system), you would use:

Quote:

`date '+%b %d %k:%M:%S'`

So, the corrected example would be:
Quote:

cat $LOG | grep "`date '+%b %d'`" | mail -s "The log from today" your.email@example.com
You'll notice that the date command is only using the month and day, as that is what you will want to match on. That will show you the log entries that match the month and day.

If you had left the %k:%M: in there, it would only match on the same hour and minute that the cron script ran on.

Anyway, you see the idea.

deoren 01-26-2005 09:10 PM

Re: yeah but
 
Quote:

Originally posted by ryedunn
wont that only return 1 line? (ie.. the date line)

I want to see everything from todays date down.

My flawed example would not have worked at all the way it was quoted. If was was quoted right, it would not have returned the entries you were looking for.

*doh*

I just realized what you were asking. I was somewhere off in left field.

In that case, have your script start off like this:

Code:

#!/bin/sh

LOG="/var/log/updates-`date '+%m%d%y'`.log"
echo "Running updates on `date`" > ${LOG}
urpmi.update updates >> ${LOG} 2>&1
urpmi --media updates --auto-select --auto >> ${LOG} 2>&1

and then at the end of the cron script, have it do this:

Code:

cat ${LOG} | mail -s "My updates-`date '+%m%d%y'`.log file" your.email@example.com
Try that out and let me know how it works. ;)

Berhanie 01-26-2005 10:27 PM

Instead of redirecting stdout to $LOG, you could tee it to $LOG. So, for example, the line
Code:

urpmi.update updates >> $LOG 2>&1
would be replaced by
Code:

urpmi.update updates 2>&1 | tee -a $LOG


All times are GMT -5. The time now is 03:25 AM.