delay of redirection of stderr within a ksh script

delay of redirection of stderr within a ksh script

Post by Jason Anderso » Sat, 15 Sep 2001 15:11:31



Hi all,

I'm writing a fairly large script that is essenially a frontend to other
programs/scripts & I want to redirect stderr to a file and see it on the
screen as well, thus allowing any error messages to be captured to file,
while also letting any prompts/messages passed through stderr to appear on
the screen.

My issue is this:
I'm using the following code which works, but I'm getting a delay of about
~2seconds between when the message is written to stderr and when it appears
on the screen (obviously because of where/how I'm sending it):

[$ErrLog is also written to by other means, such as through 'print', so
$ErrLog.screen is needed - otherwise everything I write to $ErrLog will show
on the screen, when we only want stderr]

...<declarations,etc. (incl. $ErrLog)>...
exec 2>>$ErrLog.screen
tail -f $ErrLog.screen|tee -a $ErrLog &
...<script continues>...

It is this delay I would like to get rid of, but I can't seem to reduce it.
I have tried renice-ing it with different values in the following way, but
with no apparent effect:

...<declarations,etc. (incl. $ErrLog)>...
exec 2>>$ErrLog.screen
(tail -f $ErrLog.screen|tee -a $ErrLog) &
renice -n <newvalue> $!
...<script continues>...

Can anyone help me?

OS/Shell:
UnixWare 7.1.1 i386 x86at SCO UNIX_SVR5
KSH Version M-12/28/93e-SCO

TIA
Jason

 
 
 

delay of redirection of stderr within a ksh script

Post by those who know me have no need of my nam » Sun, 16 Sep 2001 00:38:56



Quote:>I'm writing a fairly large script that is essenially a frontend to other
>programs/scripts & I want to redirect stderr to a file and see it on the
>screen as well, thus allowing any error messages to be captured to file,
>while also letting any prompts/messages passed through stderr to appear on
>the screen.

does ksh grok `process substitution', i.e.,

  >(list)

e.g.,

| $ echo foo > foo
| $ cat foo.sh
| #!/bin/bash
| exec 1> >(tee -a foo)
| exec 2>&1
| echo "test1" >&1
| echo "test2" >&2
| echo "test3" >&2
| echo "test4" >&1
| $ ./foo.sh
| test1
| test2
| test3
| test4
| $ cat foo
| foo
| test1
| test2
| test3
| test4

if that doesn't work perhaps script(1) is your friend, e.g.,

| $ echo foo > foo
| $ cat foo.sh
| #!/bin/bash
| echo "test1" >&1
| echo "test2" >&2
| echo "test3" >&2
| echo "test4" >&1
| $ SHELL=$PWD/foo.sh script -a foo
| Script started, file is foo
| test1
| test2
| test3
| test4
| Script done, file is foo
| $ cat -t foo
| foo
| Script started on Fri Sep 14 15:31:34 2001
| test1^M
| test2^M
| test3^M
| test4^M
|
| Script done on Fri Sep 14 15:31:34 2001

--
okay, have a sig then

 
 
 

delay of redirection of stderr within a ksh script

Post by David Thompso » Sun, 16 Sep 2001 09:46:10



Quote:> I'm writing a fairly large script that is essenially a frontend to other
> programs/scripts & I want to redirect stderr to a file and see it on the
> screen as well, thus allowing any error messages to be captured to file,
> while also letting any prompts/messages passed through stderr to appear on
> the screen.

> My issue is this:
> I'm using the following code which works, but I'm getting a delay of about
> ~2seconds between when the message is written to stderr and when it
appears
> on the screen (obviously because of where/how I'm sending it):

> [$ErrLog is also written to by other means, such as through 'print', so
> $ErrLog.screen is needed - otherwise everything I write to $ErrLog will
show
> on the screen, when we only want stderr]

> ...<declarations,etc. (incl. $ErrLog)>...
> exec 2>>$ErrLog.screen
> tail -f $ErrLog.screen|tee -a $ErrLog &
> ...<script continues>...

[snipped]

I've done similar things before.  The best thing I came up with was
to use tee, but I put this in a 2nd wrapper, so that your big script
has it's own tiny wrapper, like this,

--begin tiny wrapper--
  your-big-script 2>&1 | tee -a $errlog
--end tiny wrapper--

So you actually run the tiny wrapper script, and it executes your big
script using the tee to save a copy of the output.  However, this method
probably won't work for stderr only.  I recommend you look at using
some kind of coprocess, such as available in ksh.

--
David Thompson

Foster City, CA  USA

 
 
 

1. STDERR redirection on ksh

Hi guys,

        I want to redirect standard error to a file, okay that's simple but
I also want to duplicate the output of standard error to STDOUT. Please dont think the following is the answer,

        cmd 2>&1 | tee saved_file

        That's because I want to do all this stuff within the script. Right now
I can redirect all STDERR with 2>>$logfile but none of the errors can be seen on the terminal.

        I hope I was able to explain my problem. Since this script will be environment exported function all the STDERR gets saved in a file but can seen on the terminal at the same time. Oh by the way I am using ksh.

Thanks in advance
shashi

DISCLAIMER: The opinions expressed here are my own and does'nt reflect anybody.

2. Testing Computer Software Conference (TCS2001) Announcement

3. ksh stdout and stderr redirection

4. comp.os.linux.security biweekly FAQ pointer

5. stderr and stdout to a file within a shell script

6. reserve memory

7. How to print a message, from within a shell script, to its own stderr

8. is sed buggy?

9. Closing stdout and stderr, reopening them to a file within a script.

10. stdout and stderr redirection in batch csh script

11. ksh redirect stdout & stderr > file1, stderr > file2

12. HELP: how to capture stderr from within a script?

13. redirection std output and std error from within the script