title: How can I handle a broken STDOUT pipe in Elixir?
body: <p>This Elixir script outputs some numbers.</p>
  
  <pre><code># output_numbers.exs
  (1..1_000) |&gt; Enum.each(fn (i) -&gt;
    IO.puts i
  end)
  </code></pre>
  
  <p>If I run <code>elixir output_numbers.exs | head -10</code>, I get an error:</p>
  
  <pre><code>** (ErlangError) erlang error: :terminated
  </code></pre>
  
  <p>I think this is because <code>head -10</code> closes its <code>STDIN</code> pipe once it has 10 lines.</p>
  
  <p>How can I gracefully terminate in this case?</p>