David Niklas
2016-10-28 13:42:22 UTC
On Wed, Oct 19, 2016 at 6:32 PM, John McKown
the logfile in order (that problem exists even in the source program,
but also in the tee command below). But here's a command line which
{ echo "fred"; echo "wilma" 1>&2; } 2> >(tee -a logfile) 1>>logfile
Basically, it redirects stderr to a process substitution (that is, it
connects stderr to the stdin of tee). The process substitution's
stdout is of course the same as the current shell - so stderr goes both
to the log file and to the tty; then we redirect stdout to point to the
logfile as well.
I used -a on the tee command, and >> on the redirection of stdout, so
that neither would crop the log file (and presumably destroy the output
of the other), but I'm not well versed in what happens when there are
multiple open file pointers to the same file on disk. From trivial
testing, it seems to work, but I worry that perhaps there are race
conditions I haven't explored. (I originally thought about using some
magic with /dev/fd to duplicate file pointers, but I wasn't sure that
it was necessary complexity
- or that I could get it totally correct.)
Russ
Yuck, a top post (reformat).On Wed, Oct 19, 2016 at 12:28 PM, Christof Warlich
myself. The closest that I can come to what you want (and it is not
all that close) is to use the "script" program to run your program.
script -c 'somecmd p1 parm2 --option' logfile.txt
Both stdout & stderr come to the terminal & get written to
logfile.txt? . There is no way, that I know of, to put stdout into
"logfile.txt" but suppress it from the terminal.
--
Heisenberg may have been here.
Unicode: http://xkcd.com/1726/
Maranatha! <><
John McKown
As others have mentioned, there's no way to ensure that the lines reachHi,
I want my script(s) to print stderr to the terminal, while both
stdout _/and/_ stderr should go to a logfile. I thought that this
is an easy
task,I want my script(s) to print stderr to the terminal, while both
stdout _/and/_ stderr should go to a logfile. I thought that this
is an easy
but so far, I failed miserably.
Any ideas how this could be done, ideally without using any
temporary files or named pipes?
Many thanks,
Chris
?I've read all the replies. And tried some really strange thingsAny ideas how this could be done, ideally without using any
temporary files or named pipes?
Many thanks,
Chris
myself. The closest that I can come to what you want (and it is not
all that close) is to use the "script" program to run your program.
script -c 'somecmd p1 parm2 --option' logfile.txt
Both stdout & stderr come to the terminal & get written to
logfile.txt? . There is no way, that I know of, to put stdout into
"logfile.txt" but suppress it from the terminal.
--
Heisenberg may have been here.
Unicode: http://xkcd.com/1726/
Maranatha! <><
John McKown
the logfile in order (that problem exists even in the source program,
but also in the tee command below). But here's a command line which
{ echo "fred"; echo "wilma" 1>&2; } 2> >(tee -a logfile) 1>>logfile
Basically, it redirects stderr to a process substitution (that is, it
connects stderr to the stdin of tee). The process substitution's
stdout is of course the same as the current shell - so stderr goes both
to the log file and to the tty; then we redirect stdout to point to the
logfile as well.
I used -a on the tee command, and >> on the redirection of stdout, so
that neither would crop the log file (and presumably destroy the output
of the other), but I'm not well versed in what happens when there are
multiple open file pointers to the same file on disk. From trivial
testing, it seems to work, but I worry that perhaps there are race
conditions I haven't explored. (I originally thought about using some
magic with /dev/fd to duplicate file pointers, but I wasn't sure that
it was necessary complexity
- or that I could get it totally correct.)
Russ
I got a better idea.
stdbuf -oL -eL CMD 2> >(stdbuf -eL -oL tee -a logfile) 1>>logfile
This will work better (in theory if your lines are not multiline based).
Sincerely,
David