I need to capture output of a daemon that also includes a on-demand mode and behaves differently depending on if output goes to tty or not. Just redirecting stdout anywhere makes it go into logging mode where it writes data in inconvenient format and ATM it would take too much time to reconfigure it to do otherwise/fix it/ask author.
Can I somehow just run it as usual – i.e. without redirection – but still get a copy of everything it writes to screen in a file?
Best Answer
You can use
socat
to makemydaemon
's stdout a pseudo terminal device and have all the data written there sent to a pipe bysocat
.Here using
ls -l /proc/self/fd
in place ofmydaemon
See how
ls
's stdout is a new pty device (/dev/pts/26
)If you don't have
socat
, you could also usescript
:(the
< /dev/null
is so thatscript
doesn't set your terminal inraw
mode).Note however that in that case, all of stdin, stdout and stderr are redirected to that pty. For stdin and stderr to be untouched as with the
socat
approach, you could do:Not all
script
implementations/versions support the-c
or-q
option.Note that some systems come with an
unbuffer
expect
script for that but beware it has several bugs and limitations.