Operating System - HP-UX
1830895 Members
1472 Online
110017 Solutions
New Discussion

Re: Redirecting the output to logfile and Monitor

 
SOLVED
Go to solution
George Abraham_3
Occasional Advisor

Redirecting the output to logfile and Monitor

Hello All,

I have a script and i would like to have the output & errors directed to a logfile as well as the monitor.

LOGDIR=/home/log
D1=$(date "+%Y%m%d_%H%M")
exec 2>&1 | tee $LOGDIR/test.ksh.$D1


I used this comand at the begining but when a error happens it comes only in the monitor and not the logfile.

I also tried with 2>&1 at the end of the line. didt work

please help

thanks
George
3 REPLIES 3
Howard Marshall
Regular Advisor

Re: Redirecting the output to logfile and Monitor

I donâ t see a problem with what you have.

I tired it using something simple like the ls -l command
If you give ls -l two file names one real and one made up it sends the display of the real file to stdout (the screen) and the one that doesn't to stderr (also the screen in this case)

A single redirect arrow > redirects stdout to somewhere else and if you put >2&1 that redirects stderr to stdout and they both go where they are supposed to even if you pipe it to tee.

Is it possible that your program is not writing its error to stderr ? or what ever program is calling the script is trapping stderr before it gets to the pipe?

Try it outside your script just running the command from the command line and see how that works. It could be something about either the script or what ever your using to call the script

Wish I could help you out but it looks to me like what you have should work

H
George Abraham_3
Occasional Advisor

Re: Redirecting the output to logfile and Monitor

I just made a small script to emulate the problem..

========================================

LOGDIR=/home/saty069/log
D1=$(date "+%Y%m%d_%H%M")
exec 2>&1 > $LOGDIR/test.ksh.$D1
case "$1" in
A ) echo "A" ;;
B ) echo "B" ;;
C ) echo "C" ;;
* ) echo "Please check the input"
exit;;
esac
echo "script still runnng"

======================================

if i give #./test T

it displays in screen nothing comes as output on screen but in the log it shows
"Please check the input"
Muthukumar_5
Honored Contributor
Solution

Re: Redirecting the output to logfile and Monitor

You have to use only the tee to redirect STDOUT and STDERR related informations to log file and console.

Example:

# cat test.sh
(
exec 2>&1;
ls muthu;
echo "script still runnning";
) | tee -a test.log

# sh test.sh
muthu not found
script still runnning
# cat test.log
muthu not found
script still runnning

muthu not found is stderror
script still runnning is stdout

hth.
Easy to suggest when don't know about the problem!