0

I have the following script running in the background:

while true; do
    { file=$(/somescript); echo -ne "HTTP/1.0 200 OK\r\n\r\n"; cat $file; } | nc -l -p 20000
done

It works fine, except that /somescript is executed once instantly, before even any request is made to port 20000, and then it waits and responds correctly.

What am I doing wrong? It's supposed to run /somescript only when there's a request 20000

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

1 Answers1

1

Note: Question was edited (pipeline reversed) after writing this. I will update if I find time to do so.

Each part of a pipeline is started concurrently with the others. The only thing synchronising the pipeline is I/O, i.e. a process waiting for another process in the pipeline to read its output or provide input.

Your somescript script will have to wait and watch the input from nc for a request before outputting a filename.

Not knowing what the script does, I can only speculate that it may have some issues with parsing.

Kusalananda
  • 333,661
  • does it mean that nc outputs something when it starts? should I just supress errors etc.? – user224371 Apr 15 '17 at 10:53
  • 1
    also see https://unix.stackexchange.com/questions/37508/in-what-order-do-piped-commands-run – rudimeier Apr 15 '17 at 11:08
  • 1
    @user224371 I don't know what your nc does (mine doesn't output anything, and also doesn't have a -p flag). In any case, your script needs to wait until it sees the standard HTTP GET request. – Kusalananda Apr 15 '17 at 11:10