How to get the PID of a process in a pipeline

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

    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.

提交回复
热议问题