LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-24-2006, 08:25 AM   #1
emmar
LQ Newbie
 
Registered: Oct 2006
Posts: 3

Rep: Reputation: 0
stderror and cron job


I'm not sure if this is the right forum for this question but.......

I've set up a cron job on redhat and set it up to send stdout to one log file and stderr to another... for example:

0 7,13,19 * * * /root/scripttorun.sh >/root/scripttorun.log 2> /root/error.log

I'm also setting up a script to mail the error.log file if it exists

perl /root/smtp_send.pl -t user@domain.co.uk -s " Error for script on $HOSTNAME" -a /root/error.log

(smtp_send.pl uses MIME::Lite - the -t,-s -a are variables which get feed in to perl script to specify who the mail should goto, what the sibject is and what file to attach)

I really only want the cron job to create error.log if there are actually any errors but everytime the cron job runs it creates an empty error.log.

Is this what you would expect to see??? i.e will it always create an empty error.log file? Can anyone suggest a way to amend the second script so it only attched the file if is is larger than 0kb?

Thanks,

Emma
 
Old 10-25-2006, 02:01 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Cron is not the program that creates the error log, the command given to cron does. The error.log needs to be created so that errors (if any) can be written to it, which is logical if you think about it.

When executing this command: /root/scripttorun.sh >/root/scripttorun.log 2> /root/error.log the shell does 3 things (it does a lot more, but I'm keeping it simple).

1) create error.log (the 2> /root/error.log part)
2) create scripttorun.log (the >/root/scripttorun.log part)
3) scripttorun.sh is started and output can now be redirected to one of the log files.

To solve your problem you need to check if the error.log is empty and act accordingly, which probably means expanding the perl script (or create a new script that includes the smtp_send.pl functionality).

Hope this clears things up.
 
Old 10-25-2006, 05:12 AM   #3
emmar
LQ Newbie
 
Registered: Oct 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for confirming that. Wasnt sure if it was something i was doing wrong! But looks like it'll always create the error log even if no errors are passed to it.

Will try and work out a way of only running the perl script if the error log is greater than 0

Thanks for your help,

Emma
 
Old 10-25-2006, 05:32 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Bash has the -s option (True if file exists and has a size greater than zero). Something like this could be of use:

[ -s file ] && echo "File is not empty"

The echo part is only executed if the -s file part is true.

Perl has this functionality as well (it's also -s if I'm not mistaken).

Hope this helps.
 
Old 10-25-2006, 06:07 AM   #5
emmar
LQ Newbie
 
Registered: Oct 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Just found out i can use "if test -s /root/error.log" to do this.

Thanks

Emma


WHOOPS = just realised i must have missed your last post!! Thanks

Last edited by emmar; 10-25-2006 at 07:38 AM.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cron job somsahi Linux - Software 9 10-05-2006 03:19 PM
need help with cron job parthcorp1 Linux - General 2 02-01-2005 12:37 PM
cron job working2hard Programming 6 07-28-2004 10:12 PM
cron job thesnaggle Linux - Newbie 1 09-19-2003 11:47 AM
Cron job T-Rex Linux - Newbie 1 09-26-2001 12:28 PM


All times are GMT -5. The time now is 10:33 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration