Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

general question about log file 1

Status
Not open for further replies.

vlz

IS-IT--Management
Aug 11, 2002
56
IL
Hi,
I have a general question about creating log file for ksh
script.
1. I'd like to send all my script stdout and sdterr messages to the same log file.
2. I want to add a pattern before each line comes to the log file from stderr. This will done in order to recognize all messages coming from stderr.
3. I prefer to have a solution when the needed redirections
will be done at the begining of a script and not per each script line.

Thanks in advance!
Vadim
 
1. I'd like to send all my script stdout and sdterr messages to the same log file.
try exec-xxx >ouput 2>&1

2. I want to add a pattern before each line comes to the log file from stderr. This will done in order to recognize all messages coming from stderr.
no chanches
try exec-xxx >ouput 2>second
sed -s 's/.*/stderr &' second >>output

3. I prefer to have a solution when the needed redirections
will be done at the begining of a script and not per each script line.
??? what ???

don't forget, RTFMP :) guggach
 
Hi guggach,
In your example you add stderr messages at the end of the log file after stdout messages.
It will change the normal sequence of the messages.
(stdout messages can be mixed with stderr)

Regards,
Vadim

 
You could probably accomplish (2) using fifos, but it would be a little complicated. You'd still need to write stderr and stdout to separate files, otherwise you'll have them intermingled in the log. Not interweaved, line by line, like you want, but intermingled, showing up in the middle of each other's lines.

The way around this would be to use two background processes, one each for stdout and stderr, each reading a fifo. The background processes could then log each line they read, prepending a timestamp and stream indicator. Something like `date +"[%Y%m%d%H%M%S stdout]"`.

When all the processing is done, sort together the two logs to see your output properly interwoven.

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
as RodKnowlton said, write a own prog
catching and manipulating stdout + stderr
then print out

using pipe(), dup(), fork() + more
closing and redirect standard chanels ...
in 'c' no problems

don't forget, RTFMP :) guggach
 
guggach,

I was talking about doing it in ksh, using mknod, exec, and &.

Implementation is left as an exercise for the student, and heaven help the maintainer. :)

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
yes RodKnowlton, i know what you are talking about
shells are not prepared to do what vlz expects
a cheap tip: script ?

don't forget, RTFMP :) guggach
 
That depends on the granularity vlz expects.

If Vadim just wants to see errors logged within a second, either way, of any concurrent standard output, the following script could do.

I know it contains a race condition, but testing seems to indicate that the only effect is the occasional change of position of a stderr line, relative to a concurrent stdout line. No intermingling occurred in > 100 runs, possibly due to the implementation of >> redirection in my particular shells (ksh on AIX, bash on Linux). YMMV.

Code:
#!/bin/ksh
#
# testnod

ERRFIFO=err$$
OUTLOG=outlog

# make our temporary fifo
mknod $ERRFIFO p

# launch background readers
(
while read
do
echo "`date +'[%Y%m%d %T stderr]'` $REPLY" >> $OUTLOG
done < $ERRFIFO
) &


# actual script body goes within these parentheses,
# those listed are just an attempt to exercise concurrent ouput on
# both stderr and stdout
(
echo "standard out"
exec 5<&2
echo "printing to a clone of stderr" >&5
(
sleep 2
echo "inside background job"
echo "error inside background job" >&2
) &
./testnod2
/usr/bin/grep -BADFLAG &
echo "standard error" >&2
/usr/bin/who -b &
sleep 2
/etc/ifconfig  -a &
echo "another error" >&2
echo "more output"
) >>$OUTLOG 2>$ERRFIFO

wait

rm $ERRFIFO

This "testnod2" script is called by the first, to demonstrate/test that subprocess redirection works as desired.

Code:
#!/bin/ksh
#
# testnod2

echo "inside test2"

echo "error inside test2" >&2

NOTE: This just shows that it CAN be done, not that it SHOULD be done

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Hi Rod,
Thank you for helpful information.

Regards,
Vadim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top