How can I set up a process that has had its output redirected via process substitution so that it can survive after the parent script exits?
I am trying to set up a Bash script to monitor the output of a process when it starts up and then release that process so that it can continue normally (I want to call this script in other scripts and have it block further execution until start up is complete).
Right now, I am using the http.server
module as my test case. The code below properly starts up the Python module and checks its output looking for a GET
request and then exits. However, when it exits, the http.server
process also dies. How can I make it so that this process survives after bash exits?
exec 3< <(python3 -u -m http.server 2>&1 )
while true; do
read -t 2 line
echo "reading..."
echo "$line"
if [[ "$line" =~ .*GET.* ]]; then
break
fi
done <&3
I thought the process was dying because the file descriptor 3
is closed when bash exits and Python is exiting when it sees its output file closed. I have tried various redirections of 3
, but they didn't help. Maybe something else about the process needs to be modified so that it will survive? Basically I want the equivalent of executing python3 -u -m http.server 2>&1 &
but with the ability to monitor the process's output temporarily when it first starts.
pgrep
right after making aGET
request to the server thathttp.server
is still running but it is gone when I check again shortly after. What happens to file descriptor 3 when the script exits? Perhaps file descriptor 3 is closed after the script exits and when Python notices that its stdout and stderr are redirected to a closed file it also exits?