Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm assuming you want to still see STDERR and STDOUT on the terminal. You could go for Josh Kelley's answer, but I find keeping a <code>tail</code> around in the background which outputs your log file very hackish and cludgy. Notice how you need to keep an exra FD and do cleanup afterward by killing it and technically should be doing that in a <code>trap '...' EXIT</code>.</p> <p>There is a better way to do this, and you've already discovered it: <code>tee</code>.</p> <p>Only, instead of just using it for your stdout, have a tee for stdout and one for stderr. How will you accomplish this? Process substitution and file redirection:</p> <pre><code>command &gt; &gt;(tee -a stdout.log) 2&gt; &gt;(tee -a stderr.log &gt;&amp;2) </code></pre> <p>Let's split it up and explain:</p> <pre><code>&gt; &gt;(..) </code></pre> <p><code>&gt;(...)</code> (process substitution) creates a FIFO and lets <code>tee</code> listen on it. Then, it uses <code>&gt;</code> (file redirection) to redirect the STDOUT of <code>command</code> to the FIFO that your first <code>tee</code> is listening on.</p> <p>Same thing for the second:</p> <pre><code>2&gt; &gt;(tee -a stderr.log &gt;&amp;2) </code></pre> <p>We use process substitution again to make a <code>tee</code> process that reads from STDIN and dumps it into <code>stderr.log</code>. <code>tee</code> outputs its input back on STDOUT, but since its input is our STDERR, we want to redirect <code>tee</code>'s STDOUT to our STDERR again. Then we use file redirection to redirect <code>command</code>'s STDERR to the FIFO's input (<code>tee</code>'s STDIN).</p> <p>See <a href="http://mywiki.wooledge.org/BashGuide/InputAndOutput" rel="noreferrer">http://mywiki.wooledge.org/BashGuide/InputAndOutput</a></p> <p>Process substitution is one of those really lovely things you get as a bonus of choosing <code>bash</code> as your shell as opposed to <code>sh</code> (POSIX or Bourne).</p> <hr> <p>In <code>sh</code>, you'd have to do things manually:</p> <pre><code>out="${TMPDIR:-/tmp}/out.$$" err="${TMPDIR:-/tmp}/err.$$" mkfifo "$out" "$err" trap 'rm "$out" "$err"' EXIT tee -a stdout.log &lt; "$out" &amp; tee -a stderr.log &lt; "$err" &gt;&amp;2 &amp; command &gt;"$out" 2&gt;"$err" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload