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

后端 未结 6 1310
庸人自扰
庸人自扰 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 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_)

提交回复
热议问题