Patrick Schleizer
2017-02-27 00:03:00 UTC
Hi,
my goal is to duplicate, redirect all output (stdout and stderr) of an
application (apt-get) to a file while retaining the usual behavior of
apt-get, stdout and stderr.
How to create a real copy of file descriptor stdout and stderr? I would
like to keep stdout and stderr as "natural" as possible. I.e. if it was
running for example apt-get, I would wish to retain colors and progress
information.
By using `tee`, colors and progress information [1] are lost. Therefore
I experimented with using `unbuffer`, however I would appreciate if both
`unbuffer` and `tee` would not required. [2]
The following example is partially functional. (The wrapper does more
stuff, just kept it simple.)
#####
#!/bin/bash
temp_dir="$(mktemp --directory)"
logfile="$temp_dir/log"
exec 3>&1
exec 1> >(tee -a "$logfile")
#exec 4>&2
#exec 2> >(tee -a "$logfile")
unbuffer apt-get "$@"
#####
Broken: stderr redirection. Once I enable...
exec 4>&2
exec 2> >(tee -a "$logfile")
...nothing gets written to stdout anymore. Could you advice please on
how to fix this?
Cheers,
Patrick
[1]
22% [8 Packages 3,449 kB/7,098 kB 49%]
174
kB/s 4min 57s
[2] To keep the wrapper simpler and dependent on less external binaries.
Otherwise this could cause conflicts with mandatory access control and
so forth.
my goal is to duplicate, redirect all output (stdout and stderr) of an
application (apt-get) to a file while retaining the usual behavior of
apt-get, stdout and stderr.
How to create a real copy of file descriptor stdout and stderr? I would
like to keep stdout and stderr as "natural" as possible. I.e. if it was
running for example apt-get, I would wish to retain colors and progress
information.
By using `tee`, colors and progress information [1] are lost. Therefore
I experimented with using `unbuffer`, however I would appreciate if both
`unbuffer` and `tee` would not required. [2]
The following example is partially functional. (The wrapper does more
stuff, just kept it simple.)
#####
#!/bin/bash
temp_dir="$(mktemp --directory)"
logfile="$temp_dir/log"
exec 3>&1
exec 1> >(tee -a "$logfile")
#exec 4>&2
#exec 2> >(tee -a "$logfile")
unbuffer apt-get "$@"
#####
Broken: stderr redirection. Once I enable...
exec 4>&2
exec 2> >(tee -a "$logfile")
...nothing gets written to stdout anymore. Could you advice please on
how to fix this?
Cheers,
Patrick
[1]
22% [8 Packages 3,449 kB/7,098 kB 49%]
174
kB/s 4min 57s
[2] To keep the wrapper simpler and dependent on less external binaries.
Otherwise this could cause conflicts with mandatory access control and
so forth.