How to get the PID of a process in a pipeline

前端 未结 9 606
北海茫月
北海茫月 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:25

    Just had the same issue. My solution:

    process_1 | process_2 &
    PID_OF_PROCESS_2=$!
    PID_OF_PROCESS_1=`jobs -p`
    

    Just make sure process_1 is the first background process. Otherwise, you need to parse the full output of jobs -l.

提交回复
热议问题