如何以与 shell 无关、与语言无关的方式从命令行 a 获取当前的 Linux 进程 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8281345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the current Linux process ID from the command line a in shell-agnostic, language-agnostic way
提问by Johnny Utahh
How does one get their current process ID (pid) from the Linux command line in a shell-agnostic, language-agnostic way?
如何以一种与 shell 无关、与语言无关的方式从 Linux 命令行获取他们当前的进程 ID (pid)?
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.
pidof(8)
似乎没有选择获取调用进程' pid
。当然,Bash 有$$
- 但对于我的通用用法,我不能依赖 shell(Bash 或其他方式)。而且在某些情况下,我无法编写脚本或可编译程序,因此 Bash/Python/C/C++(等)将无法运行。
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.$$
).
这是一个特定的用例:我想获取pid
正在运行的基于Python-Fabric的远程 SSH 进程(可能希望避免假设 bash 正在运行),以便除其他外,我可以复制和/或创建文件和/或具有唯一文件名的目录(如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.
如果我们可以解决特定于 Fabric 的问题,那会很有帮助 - 但它并不能解决我的长期问题。对于所有未来场景中的通用用途,我只想要一个返回$$
Bash 中提供的内容的命令。
采纳答案by Johnny Utahh
Great answers + comments hereand here. Thx all. Combining both into one answer, providing two options with tradeoffs in POSIX-shell-required vs no-POSIX-shell-required contexts:
很棒的答案 + 评论在这里和这里。谢谢所有。将两者结合为一个答案,在 POSIX-shell-required 与 no-POSIX-shell-required 上下文中提供两种权衡选项:
- POSIX shell available: use
$$
- General cmdline: employ
cut -d ' ' -f 4 /proc/self/stat
- POSIX shell 可用:使用
$$
- 通用命令行:雇用
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.)
(不确定如此关注独立于 shell 的相关性/有用性,但只是经历了很多次“在没有 shell 的情况下运行系统调用”约束,现在尽可能寻求独立于 shell 的选项。)
回答by stew
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.
如果您有权访问 proc 文件系统,则 /proc/self 是当前 /proc/$pid 的符号链接。例如,您可以从 /proc/self/stat 的第一列中读取 pid。
If you are in python, you could use os.getpid().
如果你使用 python,你可以使用 os.getpid()。
回答by millimoose
Hope this is portable enough, it relies on the PPID being the fourth field of /proc/[pid]/stat
:
希望这足够便携,它依赖于 PPID 作为第四个字段/proc/[pid]/stat
:
cut -d ' ' -f 4 /proc/self/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, that cut
is a separate executable and not a shell builtin, and that cut doesn't spawn subprocesses.
它假设一个Linux与正确的形状/proc
,该布局/proc/[pid]/stat
将无法从任何Debian的6.0.1有不兼容的不同,这cut
是一个独立的可执行文件,而不是一个shell内建,而切不产卵子进程。
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:
作为替代方案,您可以使用字段 6 而不是字段 4 来获取“会话领导者”的 PID 。交互式 shell 显然将自己设置为会话领导者,并且这个 id 应该在管道和子 shell 调用中保持不变:
$ echo $(echo $( cut -f 6 -d ' ' /proc/self/stat ) )
23755
$ echo $(echo $( cut -f 4 -d ' ' /proc/self/stat ) )
24027
$ echo $$
23755
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.
也就是说,这引入了对正在运行的 shell 行为的依赖——只有当它是您真正想要的 PID 时,它才必须设置会话 ID。显然,如果您想要执行脚本的 shell 的 PID 而不是交互式的,这也不适用于脚本。
回答by duskwuff -inactive-
$$
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.
$$
不是特定于 bash 的——我相信它在所有符合 POSIX 的 shell 中都可用,这几乎相当于每个不是故意奇怪的 shell。
回答by Aleksandr Levchuk
From python:
从蟒蛇:
$ python
>>> import os
>>> os.getpid()
12252
回答by Jordan Samuels
Fewer characters and guaranteed to work:
更少的字符并保证工作:
sh -c 'echo $PPID'