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?
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.