title: Bash: how to make telnet or nc work in background until connection is closed
body: <p>Below I am describing what I was struggling with and the decision that I came to currently. <strong>Please point me to the cleverer/smaller decisions, also would be glad to receive feedback</strong>.</p>
  
  <p>So, there are a publisher and a client on a localhost, they communicate through port 8080. I can telnet or nc to this port and write output to a log normally, but cannot make the same commands work in background.</p>
  
  <p>What I see is that when launched in background they stop immediately after getting the first input (<strong><em>is it really so?</em></strong>), but in foreground they work as they should and die only after publisher closes connection at this port.</p>
  
  <p>This is what occurs normally:</p>
  
  <pre><code>&gt; telnet localhost 8080 | tee output.log (or &gt;&gt;output.log, no matter)
  Trying 127.0.0.1...
  Connected to localhost.
  Escape character is '^]'.
  </code></pre>
  
  <p>Publisher starts sending information via port.</p>
  
  <pre><code>***some necessary output***
  </code></pre>
  
  <p>Publisher closes the port.    </p>
  
  <pre><code>Connection closed by foreign host.
  </code></pre>
  
  <p>But when launched in background it stops immediately, without waiting for output:</p>
  
  <pre><code>&gt; nohup telnet localhost 8080 | tee output.log (or &lt;command&gt; &amp;, or nohup &lt;command&gt; &amp;)
  Trying 127.0.0.1...
  Connected to localhost.
  Escape character is '^]'.
  Connection closed by foreign host.
  </code></pre>