I'm attempting to build a monitoring script to watch localhost communication using netcat. I have two scripts that I've built, one to start the monitoring loop and one for the loop itself. They are as follows :


netcat localhost 1099  | bash loop.sh &


while read sInput; do
     ...do something with $sInput

Simply running this application without in the foreground works fine but as soon as I try to run this script in the background it goes from running to stopped instantly. Could someone please educate me as to why this is happening and how to alleviate the problem.

My end goal is to have a bash script that I can create an arch linux daemon script with and have everything work perfectly. Your help is appreciated.


Sounds like it's reading from standard input (stdin). Try adding the -d (Do not attempt to read from stdin) option to netcat. Or redirect on the command line:

netcat localhost 1099 <&- | bash loop.sh &

You may also want to use nohup to make sure that it won't be adversely affected when/if you exit that shell before it stops.


The netcat process is reading from its standard input. Since it's connected to the terminal, the process group that netcat is in (i.e. the pipeline) must run in the foreground.

You need to plug something at the beginning of the pipeline that will send the input that the server expects. For example, with an HTTP server, you might have something like

netcat localhost 1099 <<EOF | … &
GET HTTP/1.0 http://localhost:1099/somewhere
X-Header: wibble


If the server doesn't read any input, redirect netcat's input from /dev/null.

  • I understand your explanation but not the solution. I should mention that I'm only using netcat to read and not write. In this case, could I simply use the -d option to inhibit from using stdin? – Andrew Munro Mar 21 '12 at 0:04
  • @munroan Yes, any solution that disconnects netcat from the terminal is good. Arcege and I provided different ways of doing this. – Gilles Mar 21 '12 at 0:06
  • Thanks so much . With both of your comments I came up with this... netcat -d localhost 1099 | bash loop.sh& >/dev/null However I have one remaining issue. Despite the fact that I redirect output to null, my loop.sh script still echoes to console. Any ideas why? – Andrew Munro Mar 21 '12 at 0:34
  • 2
    @munroan You need to put the redirection before the &. What you wrote runs … | bash loop.sh in the background, then runs the command >/dev/null (i.e. the empty command, which does nothing, with its output redirected to /dev/null). – Gilles Mar 21 '12 at 0:37
  • Thanks so much for your help. For accuracy I have to give it to Arcege. – Andrew Munro Mar 21 '12 at 2:02

Have discovered an utility that promises to provide pipes without the need for a separate new daemonized bash script. May come in handy: https://github.com/flonatel/pipexec

pipexec -- [NETCAT netcat localhost 1099]  [LOOP bash loop.sh] '{NETCAT:1>LOOP:0}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.