1

In your shell (bash in my case), you can get the pid of the last backgrounded process with the $! variable:

command &
echo $!

However, I'm interested in a compound command:

commandA | commandB &
echo $!

In this case, the value of $! seems to be the PID of commandB. What I'm looking for is the PID of commandA. Is there an easy way to get it?

Brian Ferris
  • 7,557
  • 5
  • 25
  • 27

2 Answers2

2

jobs -l will list the PIDs of each member of the pipeline.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
1

Have a look to this question: How to get the PID of a process that is piped to another process in Bash?

You should find what you need there.

Community
  • 1
  • 1
MatthieuP
  • 1,116
  • 5
  • 12
  • I did find some good ideas there, thought ultimately I just went with a "ps | grep | awk" solution for pulling the pid of the first process in the chain. – Brian Ferris Dec 13 '10 at 02:33