LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-26-2005, 04:46 PM   #1
ryedunn
Member
 
Registered: Jul 2003
Location: Chicago
Distribution: Fedora, ubuntu
Posts: 458

Rep: Reputation: 30
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
 
Old 01-26-2005, 05:08 PM   #2
deoren
Member
 
Registered: Oct 2003
Location: USA
Distribution: Ubuntu
Posts: 214

Rep: Reputation: 30
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
 
Old 01-26-2005, 05:09 PM   #3
deoren
Member
 
Registered: Oct 2003
Location: USA
Distribution: Ubuntu
Posts: 214

Rep: Reputation: 30
Where you have `date`, use anything you want to match on.
 
Old 01-26-2005, 05:35 PM   #4
ryedunn
Member
 
Registered: Jul 2003
Location: Chicago
Distribution: Fedora, ubuntu
Posts: 458

Original Poster
Rep: Reputation: 30
yeah but

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

I want to see everything from todays date down.
 
Old 01-26-2005, 05:44 PM   #5
ryedunn
Member
 
Registered: Jul 2003
Location: Chicago
Distribution: Fedora, ubuntu
Posts: 458

Original Poster
Rep: Reputation: 30
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.
 
Old 01-26-2005, 09:03 PM   #6
deoren
Member
 
Registered: Oct 2003
Location: USA
Distribution: Ubuntu
Posts: 214

Rep: Reputation: 30
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.
 
Old 01-26-2005, 09:10 PM   #7
deoren
Member
 
Registered: Oct 2003
Location: USA
Distribution: Ubuntu
Posts: 214

Rep: Reputation: 30
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.
 
Old 01-26-2005, 10:27 PM   #8
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cron job that email a text file paynsr Linux - Newbie 1 03-29-2004 01:36 PM
defining an external smtp server for mailing results of chkroot cron job xfurious_mindx Linux - General 0 03-15-2004 09:23 AM
usin AWK in cron job to send dynamic ip via email digihlp Programming 2 12-10-2003 08:10 AM
Cron job , send email mikeshn Linux - General 1 09-21-2003 09:02 AM
Send Email + Txt Attachment Using Cron Job beringer Linux - Newbie 3 01-31-2003 10:23 AM


All times are GMT -5. The time now is 09:26 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration