Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

Thanks Folks

share|improve this question
    
How about "pidof imapsync"? What's wrong with it? –  pitr Jan 14 '11 at 12:17
1  
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
add comment

5 Answers

up vote 1 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
add comment
any_command args &
my_child_PID=$!

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
3  
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/whatever.pid. 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
add comment

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
add comment

Javier is correct. This page will offer more information regarding ProcessManagement.

share|improve this answer
add comment

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

imapsync &
pid=$!
wait $pid
retVal=$?
share|improve this answer
add comment

Your Answer

 
discard

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.