Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 :

start.sh

#!/bin/bash
netcat localhost 1099  | bash loop.sh &

loop.sh

#!/bin/bash
while read sInput; do
     ...do something with $sInput
done

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.

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

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.

share|improve this answer
add comment

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

EOF

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

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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