How to get the PID of a process in a pipeline

前端 未结 9 607
北海茫月
北海茫月 2020-12-03 01:51

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         


        
9条回答
  •  没有蜡笔的小新
    2020-12-03 02:34

    With inspiration from @Demosthenex's answer: using subshells:

    $ ( echo $BASHPID > pid1; exec vmstat 1 5 ) | tail -1 & 
    [1] 17371
    $ cat pid1
    17370
    $ pgrep -fl vmstat
    17370 vmstat 1 5
    

提交回复
热议问题