anubhava
anubhava

Reputation: 785098

How to get the process id of a bash subprocess on command line

I know in bash we can create subshells using round parenthesis ( and ). As per bash man page:

(list) list  is  executed  in  a  subshell environment 

Also to get the current process id we use:

echo $$

Now my question is how to get process id of a subshell created using ( and ) on command line?

If I use this:

echo $$; ( echo $$; ) 

I will get the parent shell's process id printed twice on stdout since $$ gets expanded even before subshell is created. So how to really force the lazy expansion?

[Solution should work on Mac as well not just Linux]

Update:

Suggested linked answer doesn't work since echo $BASHPID does not work on my Mac and returns blank.

Upvotes: 33

Views: 17772

Answers (7)

Tim Baverstock
Tim Baverstock

Reputation: 568

I tend to want the pid in a variable, not printed to stdout:

$ ( pid=$(bash -c 'echo $PPID'); ps -ef | grep $pid )
  502  6908  6895   0  5:50PM ttys001    0:00.00 bash
    0  6910  6908   0  5:50PM ttys001    0:00.01 ps -ef
  502  6911  6908   0  5:50PM ttys001    0:00.01 grep 6908

and more pertinently for my current use:

$ ( ( pid=$(bash -c 'echo $PPID'); ps -ef | grep $pid ) & ) & sleep 1
[1] 6958
  502  6960     1   0  5:52PM ttys001    0:00.00 bash
    0  6962  6960   0  5:52PM ttys001    0:00.01 ps -ef
  502  6963  6960   0  5:52PM ttys001    0:00.01 grep 6960

Works on MacOS bash GNU bash, version 3.2.57(1)-release (arm64-apple-darwin24) Copyright (C) 2007 Free Software Foundation, Inc.

Works on Mac zsh too, and Linux bash 4.

Upvotes: 0

Gerald Boersma
Gerald Boersma

Reputation: 31

Use homebrew to install pgrep on the Mac: brew install pgrep

Check out Link to install Homebrew.

Upvotes: 2

user4401178
user4401178

Reputation:

You can use the ppid of the parent by echoing out the BASHPID of the parent when you first enter the shell, then you background the process and can look up the pid via ppid using the parent pid.

E.g. To get the pid of a sleep 555 command backgrounded within a subshell:

(echo "$BASHPID" > /tmp/_tmp_pid_ && sleep 555 &) && ps -ho pid --ppid=$(< /tmp/_tmp_pid_)

Upvotes: 1

Zulu
Zulu

Reputation: 9265

You can do :

$ ( your_action ) &
[1] 44012

And find subprocess' PID like that :

$ echo "The sub PID : $!"
The Sub PID : 44012

$! returns the last job in background's PID. (see this manual)

Upvotes: 5

anubhava
anubhava

Reputation: 785098

Thanks to all of you for spending your valuable time in finding answer to my question here.

However I am now answering my own question since I've found a hack way to get this pid on bash ver < 4 (will work on all the versions though). Here is the command:

echo $$; ( F='/tmp/myps'; [ ! -f $F ] && echo 'echo $PPID' > $F; )

It prints:

5642
13715

Where 13715 is the pid of the subshell. To test this when I do:

echo $$; ( F='/tmp/myps'; [ ! -f $F ] && echo 'echo $PPID' > $F; bash $F; ps; )

I get this:

5642
13773
  PID   TT  STAT      TIME COMMAND
 5642 s001  S      0:02.07 -bash
13773 s001  S+     0:00.00 -bash

Telling me that 13773 is indeed the pid of the subshell.

Note: I reverted back to my original solution since as @ChrisDodd commented that echo $$; ( bash -c 'echo $PPID'; ) doesn't work Linux. Above solution of mine works both on Mac and Linux.

Upvotes: 19

masseo
masseo

Reputation: 49

This seems like it works:

(echo $$; echo  `ps axo pid,command,args | grep "$$" |awk '{ getline;print $1}'`)
14609
17365

For whatever reason, OSX is limited and doesnt come with pgrep, or one could do (which works in Linux):

 (echo $$; echo  `pgrep -P $$`) 
 14609
 17390

Upvotes: 1

Chris Dodd
Chris Dodd

Reputation: 126203

Unfortunately there's no easy way to do this prior to bash version 4, when $BASHPID was introduced. One thing you can do is to write a tiny program that prints its parent PID:

int main()
{
    printf("%d\n", getppid());
    return 0;
}

If you compile that as ppid and put it in your path, you can call it, eg:

$ (echo $$; ppid)
2139
29519
$ (x=$(ppid); echo $x)
29521

One oddness I noticed, however, is that if you write

$ (ppid)

it doesn't seem to actually run it in a subshell -- you need at least two commands inside the parentheses for bash to actually run them in a subshell.

Upvotes: 8

Related Questions