问题
I have a server script that runs mysqld and forks to continue running. As an example:
./mysqld <parameters> &
echo "Parent runs next line in script."
<do more stuff>
Why does tee wait for the child process to end before it ends itself?
EDIT:
For example, the following always hangs:
./myscript | tee -a logfile.log
回答1:
Because it can't be sure it has tee'd all the output if the child process is still running (and still has its standard output open).
Since the parent and child use the same standard output (which is connected to tee
's input, due to the pipe), there is no way for tee
to distinguish them. Since it consumes all input, both the parent and child must close their standard output (or terminate) before tee
will see and end-of-input condition.
If you want tee
to exit when the parent script does, you should redirect output of the child (to a file or to /dev/null
for example).
来源:https://stackoverflow.com/questions/18595199/why-does-tee-wait-for-all-subshells-to-finish