[PLUG] handling error in shell script

Derek Loree derek at infotects.com
Mon Jan 27 09:55:02 UTC 2003


On Fri, 2003-01-24 at 15:01, Jeme A Brelin wrote:
> On 24 Jan 2003, Derek Loree wrote:
> > I have shell script that runs as a cron job, all it does is remove a
> > file, then copy a new file to that location.  This works, except that
> > I have no way of knowing if it fails (without rooting through the log
> > files).  I can also create a script that will email a message to me.  
> > What I would like to do is have the cron job send me an email if the
> > copy command fails.
> 
> Is this not the default behavior of cron?  It sends any stderr output to
> the job owner via system mail.
> 
> You might want to just wrap the whole thing up so the error falls through
> and maybe includes an echo to stderr in the event of failure to make sure
> cron has SOMETHING to mail.

This is a good idea, do you have a short example?  I've seen a "trap"
command, but there is no man page for it.
> 
> > What I tried was to modify my ping-the-servers script, which has lines
> > in it like this (this is just one line):
> > 
> > ping -c 3 www.infotects.com >> ${LOGFILE} || echo "The web server is
> > down, fix it now!" | mail -s "Web server is down" derek at infotects.com
> 
> See, there's where I'm confused.  I'm using vixie cron and dcron and they
> both just send mail all on their own if the job exits with an error.

I need this to be more flexible, easily change the person (or people)
that gets the error message, which may not be the person that gets all
the other system errors.
> 
> > This sends me an email only if the ping command fails (that is what
> > the double pipe does, it is a command level OR).  But in my copy
> > script, I have (again just one line):
> > 
> > cp -Rp /home/test/* /home/test2 || echo "copy failed, better fix it" |
> > mail -s "Copy Failed" derek at infotects.com
> >
> > Which sends me an email even when the copy is successful!
> 
> This works just fine for me.  Perhaps you're not using the same shell?!?

I used /bin/sh as the shell.  Maybe I had a typo or something.  I won't
know until tomorrow.

> 
> > I sure could use some ideas about how to make this work.
> 
> You've got mine.

Thank you, much appreciated.

Derek Loree






More information about the PLUG mailing list