# How to duplicate stdout/stderr?

**URL:** <https://discourse.julialang.org/t/how-to-duplicate-stdout-stderr/42970>\
**Category:** General Usage\
**Created:** [July 13, 2020, 5:46am UTC](https://discourse.julialang.org/t/how-to-duplicate-stdout-stderr/42970 "2020-07-13T05:46:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samuel\_Ainsworth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuel_ainsworth/32/15644_2.png) [@Samuel\_Ainsworth](https://discourse.julialang.org/u/Samuel_Ainsworth)\
**Post date:** [July 13, 2020, 5:46am UTC](https://discourse.julialang.org/t/how-to-duplicate-stdout-stderr/42970/1 "2020-07-13T05:46:18Z")

</div>

I’m looking for a way to duplicate stdout/stderr. I’m aware of [I/O and Network · The Julia Language](https://docs.julialang.org/en/v1/base/io-network/#Base.redirect_stdout) which is great, but unfortunately it captures stdout instead of duplicating it.

In python I use:

```python
logfile = tempfile.NamedTemporaryFile()
tee = subprocess.Popen(["tee", logfile.name], stdin=subprocess.PIPE)
# Cause tee's stdin to get a copy of our stdin/stdout (as well as that of any
# child processes we spawn).
os.dup2(tee.stdin.fileno(), sys.stdout.fileno())
os.dup2(tee.stdin.fileno(), sys.stderr.fileno())

```

and that worked wonders. Is there any way to accomplish this in Julia?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [July 13, 2020, 8:37am UTC](https://discourse.julialang.org/t/how-to-duplicate-stdout-stderr/42970/2 "2020-07-13T08:37:57Z")

</div>

> [@Write to file and stdout](https://discourse.julialang.org/t/write-to-file-and-stdout/35042/3):
>
> This needs a lot more work to define a proper IO interface, but struct Tee{TIO \<: Tuple} \<: IO streams::TIO end Tee(streams...) = Tee(streams) function \_do\_tee(tee, f, xs...) for io in tee.streams f(io, xs...) end end Base.write(tee::Tee, x) = \_do\_tee(tee, write, x) Base.write(tee::Tee, x::Union{SubString{String},String}) = \_do\_tee(tee, write, x) print(Tee(stdout, stderr), "will appear twice")
