Consider the following simplified example:
my_prog|awk '...' > output.csv &
my_pid="$!" #Gives the PID for awk instead of for my_prog
sleep 10
kill $my_pid #my_prog still has data in its buffer that awk never saw. Data is lost!
In bash, $my_pid
points to the PID for awk
. However, I need the PID for my_prog
. If I kill awk
, my_prog
does not know to flush it's output buffer and data is lost. So, how would one obtain the PID for my_prog
? Note that ps aux|grep my_prog
will not work since there may be several my_prog
's going.
NOTE: changed cat
to awk '...'
to help clarify what I need.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…