复合shell命令的PID

发布于 2024-10-07 14:15:55 字数 258 浏览 0 评论 0原文

在你的 shell 中(我的例子是 bash),你可以使用 $! 获取最后一个后台进程的 pid!变量:

command &
echo $!

但是,我对复合命令感兴趣:

commandA | commandB &
echo $!

在本例中,$!的值似乎是commandB的PID。我正在寻找的是commandA 的PID。有没有简单的方法可以得到它?

In your shell (bash in my case), you can get the pid of the last backgrounded process with the $! variable:

command &
echo $!

However, I'm interested in a compound command:

commandA | commandB &
echo $!

In this case, the value of $! seems to be the PID of commandB. What I'm looking for is the PID of commandA. Is there an easy way to get it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以心动 2024-10-14 14:15:55

jobs -l 将列出管道中每个成员的 PID。

jobs -l will list the PIDs of each member of the pipeline.

伤感在游骋 2024-10-14 14:15:55

看看这个问题:
如何在 Bash 中获取通过管道传输到另一个进程的进程的 PID?

您应该在那里找到您需要的内容。

Have a look to this question:
How to get the PID of a process that is piped to another process in Bash?

You should find what you need there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文