0

I can run these two commands:

nc -l localhost 8888 -v >> output.txt &
(while true;do sleep 1;echo a;done)|nc localhost 8888 &

All works as expected with output.txt filling with "a"s (tailed from different terminal). But if type a single character into the terminal running the background jobs the sending nc turns from Running to Stopped:

$ jobs
[1]+  Stopped                 nc -l localhost 8888 -v >> output.txt
[2]-  Running                 ( while true; do
    sleep 1; echo a;
done ) | nc localhost 8888 &

Also note it looses its "backgrounding" ampersand. Don't know where to begin debugging this. Is this expected behaviour? Thanks.

Mac OSX 10.9, GNU Bash 3.2.52

AJP
  • 143
  • 1
  • 7

1 Answers1

1

netcat is bidirectional. It copies from the socket to stdout, and to the socket from stdin. When you press a key, it notices that data is available on stdin (the tty) and tries to read it. Background jobs aren't allowed to read from the tty, so it gets suspended.

If you don't intend to send any data on the socket, you should add </dev/null to your netcat command.

And it's normal for the jobs command to add or remove the ampersand when a job changes state.