The PID of a bash child proccess (imapsync) - how do you get it?

Can any one tell me how to get the PID of a command executed in bash.

E.g. I have a bash script that runs imapsync.

When the script is killed the imapsync process does not always get killed, so I'd like to be able to identify the PID of imapsync programatically from my script, so that I can kill the imapsync process myself in a signal handler.

So how do I programatically get the PID of a child process from a parent bash script?


imapsync has an option to set where its pid is written:

--pidfile : the file where imapsync pid is written.


any_command args &
my_child_PID=$!

IOW, just like $$ holds your PID, $! has the PID of the most recently executed background command.