Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
How about "pidof imapsync"? What's wrong with it? – pitr Jan 14 '11 at 12:17
I did not know about pidof. However now that I do, it is not useful for this scenario as far as I can tell, because pidof seems to return all proccesses of the given command on a system - even those owned by other scripts/users. In addition imapsync is actually a perl script, so I would be doing a pidof perl, which again is likely to return processes that are not imapsync and may belong to other users. – Jason Tan Jan 14 '11 at 12:29
Javier is correct. This page will offer more information regarding ProcessManagement. – sinping Jan 14 '11 at 15:10
up vote 4 down vote accepted

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

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

share|improve this answer
FYI this does not really answer my question in a general sense, but I did discover the --pidfile arg and with some faffing around it has been good enough so far.. – Jason Tan Feb 8 '11 at 14:56
@JasonTan why teach a man to fish, when you can give him a tasty pre-cooked fillet, so he comes back and nags you for more, tomorrow, eh? sorry for the old post, but why accept this answer over Javiers's ? – unsynchronized May 16 at 13:38
any_command args &

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

share|improve this answer
Thanks, but imapsync is not backgrounded and $! is not set if I echo it on the next line after the imapsync invocation or the first line of the signal handler. :-( – Jason Tan Jan 14 '11 at 12:37
if it forks and backgrounds the child, it's the parent's responsibility to save the child's PID somewhere. usual practice is saving it somewhere like /var/run/ Maybe there's a command-line option to ask it to save the PID somewhere; if not, that sounds like a feature to ask the author, or implement yourself. – Javier Jan 14 '11 at 12:51
Emphasis on backgrounded. If no process has been placed into the background (i.e., with appended &), then the $! variable is undefined. – Trevor Aug 14 '14 at 15:16
More similar solutions are suggested in (an answer to) How to get pid of just started process; as an addition to this answer, a comment there says: oh, and the "oneliner": /bin/sh -c 'echo $$>/tmp/ && exec program args' & – sysfault Nov 24 '10 at 14:28 – imz -- Ivan Zakharyaschev Jun 2 '15 at 14:36

Unlike pidof, pgrep can be given a variety of options to restrict which processes it returns PIDs for. One that may be particularly useful is to select based on PPID using the PID of the current process.

pgrep -P $$ imapsync

which will only output PIDs of imapsync if they are children of your script.

share|improve this answer

How about putting imapsync in the background momentarily, getting the PID and then foregrounding it... Something like this:

imapsync &
wait $pid
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.