获取当前子shell的PID [英] Get pid of current subshell

查看:206
本文介绍了获取当前子shell的PID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让当前正在执行的子shell的pid - 但 $ 只返回父PID:

 #!的/ usr / bin / sh的X() {
  回声我是一个子shell点¯x回声1,我的pid是$$
}Y(){
  回声我是一个子shellŸ回声1,我的pid是$$
}
回声我父shell和我的pid是$$
X'放大器;
回声刚刚推出x和PID为$!Y'放大器;
回声刚刚推出Y和PID为$!等待

输出

 我的母贝和我的pid是3107
刚刚推出x和PID为3108
我是一个子shell点¯x回声1,我的PID为3107
刚刚推出y和PID为3109
我是一个子shellŸ回声1,我的PID为3107

正如你可以在上面看到,当我运行 $ 从我的后台运行功能,它不显示PID,当我做 $!从父shell。


解决方案

现代庆典

如果您正在运行bash V4或更好,子shell的PID是 $ BASHPID 可用。例如:

  $回声$$ $ BASHPID; (回声$$ $ BASHPID)
32326 32326
32326 1519

在主壳, $ BASHPID 相同 $ 。在子shell,它更新到子shell的PID。

旧的bash(版本3.x或更早)

pre版本4,你需要一个<一个href=\"http://stackoverflow.com/questions/9119885/how-to-get-the-process-id-of-a-bash-subprocess-on-command-line\">workaround:

  $回声$$; (:;庆典-c'回声$ PPID')
11364
30279

(帽尖:kubanczyk)

为什么冒号?

注意,没有冒号,该工作围绕做的不可以工作

  $回声$$; (bash的-c'回声$ PPID')
11364
11364

看来,在上面,一个子shell是永远不会创建,因此第二个语句返回主shell的PID。相反,如果我们把两个语句的括号内,创建子shell,输出是如我们预期。这是真实的,即使对方说法仅仅是一种结肠,。在外壳中,是一个无操作:它什么都不做。确实,在我们的例子然而,迫使这足以实现我们想要的子shell的创建。

短跑

在Debian类系统,破折号是默认的shell( / bin / sh的)。在 PPID 办法适用于破折号但另一个转折:

  $回声$$; (破折号-c'回声$ PPID')
5791
5791
$回声$$; (:;破折号-c'回声$ PPID')
5791
5791
$回声$$; (破折号-c'回声$ PPID';:)
5791
20961

使用破折号,放置:该命令之前命令是不够的,但把它放在后就是

POSIX

PPID 包含在的 POSIX规范

可移植性

mklement0报道,以下作品是庆典破折号 的zsh 而不是 KSH

 回声$$; (SH -c'回声$ PPID'和;&安培; :)

I am trying to get the pid of a currently executing subshell - but $$ is only returning the parent pid:

#!/usr/bin/sh

x() {
  echo "I am a subshell x echo 1 and my pid is $$"
}

y() {
  echo "I am a subshell y echo 1 and my pid is $$"
}


echo "I am the parent shell and my pid is $$"
x &
echo "Just launched x and the pid is $! "

y &
echo "Just launched y and the pid is $! "

wait

Output

I am the parent shell and my pid is 3107
Just launched x and the pid is 3108
I am a subshell x echo 1 and my pid is 3107
Just launched y and the pid is 3109
I am a subshell y echo 1 and my pid is 3107

As you can see above, when I run $$ from the function that I've backgrounded, it does not display the PID as when I do $! from the parent shell.

解决方案

Modern bash

If you are running bash v4 or better, the PID of the subshell is available in $BASHPID. For example:

$ echo $$ $BASHPID ; ( echo $$ $BASHPID  )
32326 32326
32326 1519

In the main shell, $BASHPID is the same as $$. In the subshell, it is updated to the subshell's PID.

Old bash (Version 3.x or Earlier)

Pre version 4, you need a workaround:

$ echo $$; ( : ; bash -c 'echo $PPID' )
11364
30279

(Hat tip: kubanczyk)

Why the colon?

Notice that, without the colon, the work-around does not work:

$ echo $$; ( bash -c 'echo $PPID' )
11364
11364

It appears that, in the above, a subshell is never created and hence the second statement returns the main shell's PID. By contrast, if we put two statements inside the parens, the subshell is created and the output is as we expect. This is true even if the other statement is a mere colon, :. In shell, the : is a no-operation: it does nothing. It does, in our case however, force the creation of the subshell which is enough to accomplish what we want.

Dash

On debian-like systems, dash is the default shell (/bin/sh). The PPID approach works for dash but with yet another twist:

$ echo $$; (  dash -c 'echo $PPID' ) 
5791
5791
$ echo $$; ( : ; dash -c 'echo $PPID' )
5791
5791
$ echo $$; (  dash -c 'echo $PPID'; : )   
5791
20961

With dash, placing the : command before the command is not sufficient but placing it after is.

POSIX

PPID is included in the POSIX specification.

Portability

mklement0 reports that the following works as is with bash, dash, and zsh but not ksh:

echo $$; (sh -c 'echo $PPID' && :)

这篇关于获取当前子shell的PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆