问题描述
考虑以下简化的例子:
my_prog | awk的'...'> output.csv&
my_pid =$! #Gives将PID awk的,而不是为my_prog
睡眠10
杀$ my_pid #my_prog仍然在其缓冲了awk从未见过的数据。数据丢失!
在bash中, $ my_pid
指向PID为 AWK
。不过,我需要为 my_prog
的PID。如果我杀了 AWK
, my_prog
不知道刷新它的输出缓冲器和数据丢失。那么,怎么会获得 my_prog
的PID?需要注意的是的ps aux |。grep的my_prog
将无法工作,因为可能有几个回事 my_prog
请注意:修改猫
到的awk'...'
来帮助澄清我的需要。
我能够用明确的命名管道来解决它 mkfifo子
。
步骤1: mkfifo子捕获
第2步:运行此脚本
my_prog>捕捉&
my_pid =$! #Now,我将PID为my_prog!
awk的'...'捕捉> out.csv&
睡眠10
杀$ my_pid #kill my_prog
等待#WAIT awk的完成。
我不喜欢有一个mkfifo子的管理。希望有人有一个简单的解决方案。
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.
I was able to solve it with explicitly naming the pipe using mkfifo
.
Step 1: mkfifo capture
.
Step 2: Run this script
my_prog > capture &
my_pid="$!" #Now, I have the PID for my_prog!
awk '...' capture > out.csv &
sleep 10
kill $my_pid #kill my_prog
wait #wait for awk to finish.
I don't like the management of having a mkfifo. Hopefully someone has an easier solution.
这篇关于如何得到一个进程的PID在管线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!