0

I am working with the following block of bash and it is not printing out the first line of expected output:

    exec 3< <(python3 -m http.server)
    while true; do
            read -t 2 line
            echo "reading..."
            echo "$line"
    done <&3

When python3 -m http.server starts, it prints out a line Serving HTTP on 0.0.0.0 port 8000, but the code above does not print that line. Afterwards, python will print things like GET requests as they occur and those are printed. Why is bash missing this first line?

My goal is to put this in a script and replace the echo calls with a test for the redirected process indicating that it has finished starting up and then to break the loop and end the script (so block execution of the script until start up is done), with the redirected process continuing in the background. http.server is just my test case.

1

That's because:

  • python performs output buffering if redirected to a pipe or a file.
  • http.server writes the first line to stdout then access logs to stderr.

You need to do like:

exec 3< <(python3 -u -m http.server 2>&1)

Speaking about your goal, I suppose the script needs to continue reading from the pipe even after it's gotten that specific output, because the subprocess will stop running as soon as its output to the pipe gets blocked.

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.