www.delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2004/03/03/10:13:33

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-ID: <20040303151319.57080.qmail@web60306.mail.yahoo.com>
Date: Wed, 3 Mar 2004 07:13:19 -0800 (PST)
From: Patrick Samson <p_samson AT yahoo DOT com>
Subject: Re: stdout/stderr handling by cygrunsrv/bash/tcl
To: cygwin AT cygwin DOT com
In-Reply-To: <Pine.GSO.4.56.0403030844550.17984@slinky.cs.nyu.edu>
MIME-Version: 1.0
X-IsSubscribed: yes

--- Igor Pechtchanski wrote:
> On Wed, 3 Mar 2004, Patrick Samson wrote:
> 
> > Your demo works as expected. But TCL introduces
> > the mess.
> >
> > I changed the script to:
> >
> > $ cat service.sh
> > #!/bin/bash
> > # the next line restart using tclsh \
> > exec tclsh "$0" "$@"
> >
> > puts "Starting service"
> > while {true} {
> >   # wait 1 sec
> >   after 1000
> >   puts stderr "STDERR"
> >   puts "STDOUT"
> > }
> > $ cygrunsrv -S test-daemon; sleep 3; cygrunsrv -E
> > test-daemon
> > $ cat /var/log/test-daemon.log
> > STDERR
> > STDERR
> > STDERR
> > STDERR
> >
> > STDOUT
> > STDOUT
> >
> > It looks like the overwriting of these two lines
> > (cr lf are replaced by space):
> > STDERR  STDERR  STDERR  STDERR
> > Starting service  STDOUT  STDOUT  STDOUT  STDOUT
> >
> > Just for the understanding, another trial with the
> > script beginning with a write to stderr:
> >
> > $ cat service.sh
> > #!/bin/bash
> > # the next line restart using tclsh \
> > exec tclsh "$0" "$@"
> >
> > puts stderr "Write stderr"
> > puts "Starting service"
> > while {true} {
> >   # wait 1 sec
> >   after 1000
> >   puts stderr "STDERR"
> >   puts "STDOUT"
> > }
> > $ cygrunsrv -S test-daemon; sleep 3; cygrunsrv -E
> > test-daemon
> > $ cat /var/log/test-daemon.log
> > Starting serviSTDESTDOSTDESTDOSTDESTDOSTDESTDOUT
> >
> > Again the overwriting of these two lines:
> > Write stderr  STDERR  STDERR  STDERR  STDERR
> > Starting service  STDOUT  STDOUT  STDOUT  STDOUT
> >
> > If I run the TCL directly, it's ok, no surprise:
> > $ ./service.sh
> > Write stderr
> > Starting service
> > STDERR
> > STDOUT
> > STDERR
> > STDOUT
> > STDERR
> > STDOUT
> > <manually interruped with ctrl/c>
> > $ ./service.sh >service.log 2>&1
> > <manually interruped with ctrl/c>
> > $ cat service.log
> > Write stderr
> > Starting service
> > STDERR
> > STDOUT
> > STDERR
> > STDOUT
> > STDERR
> > STDOUT
> >
> > I wouldn't use this syntax, but it shows the same
> > problem, so it may be a clue:
> >
> > $ ./service.sh >service.log 2>service.log
> > <manually interruped with ctrl/c>
> > $ cat service.log
> > Starting serviSTDESTDOSTDESTDOSTDESTDOUT
> >
> > Continuing on this way, consider:
> > serviceSH.sh as the shell script of Igor,
> > serviceTCL.sh as the TCL script.
> >
> > $ ./serviceSH.sh >service.log 2>service.log
> > trap -- 'terminateSTDERR
> > STDERR
> > Terminating
> >
> > $ ./serviceSH.sh >>service.log 2>>service.log
> >
> > Starting service
> > trap -- 'terminate' SIGINT
> > STDERR
> > STDOUT
> > STDERR
> > STDOUT
> > STDERR
> > STDOUT
> > Terminating
> >
> > $ ./serviceTCL.sh >service.log 2>service.log
> >
> > Starting serviSTDESTDOSTDESTDOSTDESTDOUT
> >
> > $ ./serviceTCL.sh >>service.log 2>>service.log
> >
> > Starting serviSTDESTDOSTDESTDOSTDESTDOSTDESTDOUT
> >
> > Can someone tell us how cygrunsrv manages the
> > redirections? It's a step toward the explanation.
> 
> cygrunsrv redirects each file separately (like the
> '>>a 2>>a' syntax).
> This is obviously a Tcl problem.  It seems that Tcl
> attempts to manage its
> own buffering on STDOUT/STDERR, even when they are
> redirected.
> 
> Try adding an 'exec 2>&1' just before the 'exec
> tclsh "$0" "$@"' in your serviceTCL.sh script.  
> Hopefully, Tcl will inherit the stderr from the
> parent process...  If this works, you can add it to
> your main script, since you know that both stdout
> and stderr will eventually go to the same file
> anyway.

Yes! It works.

$ cat serviceTCL.sh
#!/bin/bash
# \
exec 2>&1
# the next line restart using tclsh \
exec tclsh "$0" "$@"

puts stderr "Write stderr"
puts "Starting service"
while {true} {
  # wait 1 sec
  after 1000
  puts stderr "STDERR"
  puts "STDOUT"
}

$ ./serviceTCL.sh >>service.log 2>>service.log
or
$ cygrunsrv -S test-daemon; sleep 3; cygrunsrv -E
test-daemon
give the correct outputs.

Thanks a lot, Igor.
I will check the problem with the Tcl team.



__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you’re looking for faster
http://search.yahoo.com

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019