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

后端 未结 6 1307
庸人自扰
庸人自扰 2020-12-15 03:47

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

(list) list  is  executed  in  a  subs         


        
相关标签:
6条回答
  • 2020-12-15 03:59

    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)

    0 讨论(0)
  • 2020-12-15 03:59

    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
    
    0 讨论(0)
  • 2020-12-15 04:02

    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.

    0 讨论(0)
  • 2020-12-15 04:06

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

    Check out http://mxcl.github.com/homebrew/ to install Homebrew.

    0 讨论(0)
  • 2020-12-15 04:14

    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_)

    0 讨论(0)
  • 2020-12-15 04:25

    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.

    0 讨论(0)
提交回复
热议问题