cancel
Showing results for 
Search instead for 
Did you mean: 

Error log and cron job

 
Vivek_3
Advisor

Error log and cron job

hi,

i have cron job that runs every minute.

* * * * * /script/m2o.sh >> /log/m2o.log 2>/log/m2o_err.log

it generates a empty error log every min.. but i want to generate log only if there is any error.

To be more specific. /script/m2o.sh is a very simple script. It can't produce any error but based on a specific condition it does call another script (probably twice a day) which may produce some errors. i want to log those errors.

please help me to resolve this problem.

Thanks

Vivek
8 REPLIES 8
Craig Rants
Honored Contributor

Re: Error log and cron job

When you call script 2 from script 1 put your error log entry there.

i.e.

script1...
/usr/local/bin/script2 >> /log/script2.out 2>/log/script2_error.out

I think this is what you want.

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
Vivek_3
Advisor

Re: Error log and cron job

HI Craig,

Even if i applied your chnages, it is still creating a empty error log in case if there were no errors.

i want to create log if and only if there were some errors.

Hope you can help.

thanks
Steve Labar
Valued Contributor

Re: Error log and cron job

In your script that creates a log try adding an if statement around the log create to verify there was an error.
ex.
if ( $? -ne 0 ) ; then
cat {your text here} >> error.log
fi
Vivek_3
Advisor

Re: Error log and cron job

Hi steve,

so you want to say that i have to generate error myself in script through code and then redirect them to a file. i can't catch the errors generated by script it self.

Waiting for reply.

Thanks
Paula J Frazer-Campbell
Honored Contributor

Re: Error log and cron job

Hi

When redirecting output to a log watch the redirect that you use:-

> will create a fresh log file each time
>> will append to a log file.

Your problem could be that you are creating a fresh log file each time.

So for the 2nd script the redirect of >> should be used so that on the next run the error data is not overwritten.

Paula
If you can spell SysAdmin then you is one - anon
Vivek_3
Advisor

Re: Error log and cron job

hi Paula,

I need to create a fresh error log evry time script runs but i need to create error log if and only if there were some errors.

Any Help ?

thanks
Steve Labar
Valued Contributor

Re: Error log and cron job

You don't necessarily need to create an error condition, but you need to test if the error condition you want exists. If you described the error condition it might be easier to recommend a test. Some ideas may be
( -f /path/errorlog ) -if error log file exists

( $?ERRORVARIABLE ) -if error variable exists

Another way might be to simply redirect your errout during the command inside the script.
{command} 2> errorlog
This will only send messages to the log in an error state.

Good Luck.
Steve
Mark Vollmers
Esteemed Contributor

Re: Error log and cron job

Vivek-

You could also take and either have a second script or append to the first one a check, after the log is created, where if the logfile is empty, then delete, and if it has an entry, append to your permanent log. Something like:

if [ test -s "logfile" ]
then
rm logfile
else
cat logfile >> permlogfile

this way it will always so the checking and delete the empty file.

Mark
"We apologize for the inconvience" -God's last message to all creation, from Douglas Adams "So Long and Thanks for all the Fish"