5

I'm using bash on a RH Linux system.

Normally, you can get your own PID with the variable $$. However, if a script runs one of its own functions as a background process - that doesn't work; all functions run in the background will get the PID of the parent script when $$ is used.

For example, here is a test script:

    /tmp/test:
    #!/bin/bash
    echo "I am $$"
    function proce {
      sleep 3
      echo "$1 :: $$"
    }

    for x in aa bb cc; do
      eval "proce $x &"
      echo "Started: $!"
    done

When it is executed:

    /tmp$ ./test
    I am 5253
    Started: 5254
    Started: 5256
    Started: 5258
    /tmp$ aa :: 5253
    bb :: 5253
    cc :: 5253

So - the parent script (/tmp/test) executes as PID 5253 and fires-off three background processes with PIDs 5254, 5256 and 5258. But each of those background processes gets the value 5253 with $$.

How can these processes discover its actual PID?

  • Just a pedantic note: your example does not background the subshells, everything is done in the foreground. – hildred Apr 7 '15 at 4:10
9

$BASHPID may be what you are looking for.

BASHPID

Expands to the process ID of the current Bash process. This differs from $$ under certain circumstances, such as subshells that do not require Bash to be re-initialized.

As opposed to $$

($$) Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the invoking shell, not the subshell.

http://www.gnu.org/software/bash/manual/bashref.html#Bash-Variables

6

With bash:

echo "$BASHPID"

would give you the pid of the process that evaluates that echo command

Note that (for instance after a enable -n echo) it is not necessarily the same as the pid of the process that is running that echo command.

bash (or any shell) does its own soup with processes. It's not always useful to try and guess what process does what.

$ readlink -f /proc/self "/proc/$BASHPID"
/proc/30868
/proc/30747
$ (readlink -f /proc/self "/proc/$BASHPID")
/proc/30869
/proc/30869

In that second case, readlink is executed in the same process that interpreted the command line because that's the last command run in that subshell (so bash optimises out a fork()).

The zsh equivalent is in the zsh/system module:

$ zmodload zsh/system
$ echo $$ $sysparams[pid]
5155 5155
$ (echo $$ $sysparams[pid])
5155 30979

(zsh also exposes the parent pid of the subshell in $sysparams[ppid]).

Portably, you can do:

pid=$(sh -c 'echo "$PPID"')

All shells should run that sh as a direct child of the subshell interpreting that command line, so that sh's $PPID should be that subshell.

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.