如何以与 shell 无关、与语言无关的方式从命令行获取当前 Linux 进程 ID
如何以一种与 shell 无关、与语言无关的方式从 Linux 命令行获取当前进程 ID (pid)?
pidof(8)
似乎没有选项来获取调用进程'pid
。当然,Bash 有 $$
- 但对于我的通用用法,我不能依赖 shell(Bash 或其他)。在某些情况下,我无法编写脚本或可编译程序,因此 Bash / Python / C / C++(等)将无法工作。
这是一个具体的用例:我想获取正在运行的 Python-Fabric- 的 pid
基于远程 SSH 进程(人们可能希望避免假设 bash 正在运行),这样我就可以复制和/或创建具有唯一文件名的文件和/或目录(如 mkdir 中所示) /tmp/mydir.$$)。
如果我们能够解决 Fabric 特定的问题,那就很有帮助 - 但它并不能解决我的长期问题。对于未来所有场景中的通用用途,我只想要一个返回 $$
在 Bash 中提供的命令。
How does one get their current process ID (pid) from the Linux command line in a shell-agnostic, language-agnostic way?
pidof(8)
appears to have no option to get the calling process' pid
. Bash, of course, has $$
- but for my generic usage, I can't rely on a shell (Bash or otherwise). And in some cases, I can't write a script or compilable program, so Bash / Python / C / C++ (etc.) will not work.
Here's a specific use case: I want to get the pid
of the running, Python-Fabric-based, remote SSH process (where one may want to avoid assuming bash is running), so that among other things I can copy and/or create files and/or directories with unique filenames (as in mkdir /tmp/mydir.$$
).
If we can solve the Fabric-specific problem, that's helpful - but it doesn't solve my long-term problem. For general-purpose usage in all future scenarios, I just want a command that returns what $$
delivers in Bash.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
来自蟒蛇:
From python:
$$
不是特定于 bash 的——我相信它在所有 POSIX 兼容的 shell 中都可用,这相当于几乎所有不是故意变得奇怪的 shell。$$
isn't bash-specific -- I believe that it's available in all POSIX-compliant shells, which amounts to pretty much every shell that isn't deliberately being weird.希望这足够便携,它依赖于 PPID 作为
/proc/[pid]/stat
的第四个字段:它假设 Linux 具有正确的
/proc
形状,/proc/[pid]/stat
的布局不会与 Debian 6.0.1 的布局有不兼容的不同,cut
是一个单独的可执行文件,而不是一个shell 内置,并且该剪切不会生成子流程。作为替代方案,您可以获取字段 6 而不是字段 4 来获取 “会议领导者”。交互式 shell 显然将自己设置为会话领导者,并且该 id 在管道和子 shell 调用之间应该保持相同:
也就是说,这引入了对正在运行的 shell 的行为的依赖 - 仅当它是会话 ID 时,它才必须设置会话 id你真正想要谁的PID。显然,如果您想要执行脚本的 shell 的 PID,而不是交互式脚本的 PID,那么这在脚本中也不起作用。
Hope this is portable enough, it relies on the PPID being the fourth field of
/proc/[pid]/stat
:It assumes a Linux with the right shape of
/proc
, that the layout of/proc/[pid]/stat
won't be incompatibly different from whatever Debian 6.0.1 has, thatcut
is a separate executable and not a shell builtin, and that cut doesn't spawn subprocesses.As an alternative, you can get field 6 instead of field 4 to get the PID of the "session leader". Interactive shells apparently set themselves to be session leaders, and this id should remain the same across pipes and subshell invocations:
That said, this introduces a dependency on the behaviour of the running shell - it has to set the session id only when it's the one whose PID you actually want. Obviously, this also won't work in scripts if you want the PID of the shell executing the script, and not the interactive one.
很好的答案+评论这里和在这里。谢谢大家。将两者结合成一个答案,在需要 POSIX-shell 和不需要 POSIX-shell 的上下文中提供两种权衡的选项:
$$
cut -d ' ' -f 4 /proc/self/stat
使用两种方法的示例会话(以及与其他建议的非工作方法)显示此处。
(不确定如此关心 shell 独立性有多么相关/有用,但只是经历过多次“没有 shell 运行系统调用”约束,现在只要有可能就会寻求独立于 shell 的选项。)
Great answers + comments here and here. Thx all. Combining both into one answer, providing two options with tradeoffs in POSIX-shell-required vs no-POSIX-shell-required contexts:
$$
cut -d ' ' -f 4 /proc/self/stat
Example session with both methods (along with other proposed, non-working methods) shown here.
(Not sure how pertinent/useful it is to be so concerned with being shell independent, but have simply experienced many times the "run system call without shell" constraint that now seek shell-independent options whenever possible.)
更少的字符并且保证可以工作:
Fewer characters and guaranteed to work:
如果您有权访问 proc 文件系统,则 /proc/self 是当前 /proc/$pid 的符号链接。例如,您可以从 /proc/self/stat 的第一列中读取 pid。
如果你使用Python,你可以使用os.getpid()。
If you have access to the proc filesystem, then /proc/self is a symlink to the current /proc/$pid. You could read the pid out of, for instance, the first column of /proc/self/stat.
If you are in python, you could use os.getpid().