?
Solved

bash pipe to log and stderr

Posted on 2005-04-24
2
Medium Priority
?
2,541 Views
Last Modified: 2008-01-09
I have a bash script that runs and outputs its logging info to $LOGDIR/$LOGFILE. However, if I run it from cron, I want stderr to be sent both to the log file and also to stderr so that I'll get emailed about the problem.

To put it another way, if I output to the log file by doing
  doFooStuff >> $LOGDIR/$LOGFILE
then it'll output stdout to the log file and stderr to cron email that I'll get.

If I do:
  doFooStuff >& $LOGDIR/$LOGFILE
then it'll output both stdout and stderr to the log file, but I'll never get any cron email.

What can I do? I can't grep the log for error messages because there's no guaranteed string that will indicate an error.

I know that "tee" could somehow be used for stdout. But, I don't want stdout sent in the cron emails. I only want stderr.
0
Comment
Question by:HappyEngineer
2 Comments
 
LVL 23

Expert Comment

by:Mysidia
ID: 13856035
command_here   2>&1 1<&- 1>/dev/null | tee -a logfile 1>&2


Basically, this involves   moving stderr to stdout, closing descriptor 1,
re-opening  descriptor 1 to /dev/null,  calling tee, and
duplicating stdout back to stderr.
0
 
LVL 38

Accepted Solution

by:
wesly_chen earned 2000 total points
ID: 13856332
Say, your is called script.sh. Use another script to wrap it, say wrap.sh
--- wrap.sh ---
#!/bin/sh

LOGDIR=/path-to-log-dir
LOGFILE=<log file name>_`date +%m%d%Y`

/path-to-script.sh  > $LOGDIR/$LOGFILE  2>&1
mail -s "log file"  <your email address>  < $LOGDIR/$LOGFILE  
------------------
For redirection, the syntax is read from "right-to-left", it the syntax is
/path-to-script.sh  > $LOGDIR/$LOGFILE  2>&1
where 2>&1 will be read first and it tells that stdout plus stderr will redirect to $LOGDIR/$LOGFILE.

In cron job,
* * * * * /path-to-wrap.sh > /dev/null 2>&1
since your log file is sent inside the wrap.sh. No need to send through cron daemon.
This way you can control the emails being sent to whom.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question