1

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?

2

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! – Aquarius Power Jun 9 '13 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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