2

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?

1 Answer 1

3

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?

1
  • worked, here a simple example function FUNCtst() { echo "$$,$BASHPID"; };export -f FUNCtst;FUNCtst;FUNCtst&, thx! Jun 9, 2013 at 17:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .