Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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

I am trying to know the pid of a function that was run as child process on a script

FUNCtstPsSelf.sh

#!/bin/bash
echo "pid=$$;ppid=$PPID"
ps -p $$
function FUNCtstPsSelf() { 
    sleep 3;
    echo "$FUNCNAME:pid=$$;ppid=$PPID";
    ps -p $$ -o pid,ppid,command; 
}
FUNCtstPsSelf&
sleep 1;

but it can't find the pid! what now?

share|improve this question
up vote 2 down vote accepted

Try $BASHPID instead of $$. One of the two occurrences of $$ in the man page is to explain when it differs from $BASHPID, including "subshells that do not require bash to be re-initialized", which may include your background function processes?

share|improve this answer
    
worked, here a simple example function FUNCtst() { echo "$$,$BASHPID"; };export -f FUNCtst;FUNCtst;FUNCtst&, thx! – Aquarius Power Jun 9 '13 at 17:31

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.