简体   繁体   English

如何从命令行获取当前的Linux进程ID,与shell无关,语言无关

[英]How to get the current Linux process ID from the command line a in shell-agnostic, language-agnostic way

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 . pidof(8)似乎没有选择让调用进程' pid Bash, of course, has $$ - but for my generic usage, I can't rely on a shell (Bash or otherwise). Bash当然有$$ - 但是对于我的通用用法,我不能依赖shell(Bash或其他)。 And in some cases, I can't write a script or compilable program, so Bash / Python / C / C++ (etc.) will not work. 在某些情况下,我无法编写脚本或可编译程序,因此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.$$ ). 这是一个特定的用例:我想获得正在运行的基于Python-Fabric的远程SSH进程的pid (人们可能希望避免假设bash正在运行),以便除其他外我可以复制和/或创建具有唯一文件名的文件和/或目录(如mkdir /tmp/mydir.$$ )。

If we can solve the Fabric-specific problem, that's helpful - but it doesn't solve my long-term problem. 如果我们可以解决特定于Fabric的问题,这会有所帮助 - 但它并不能解决我的长期问题。 For general-purpose usage in all future scenarios, I just want a command that returns what $$ delivers in Bash. 对于所有未来场景中的通用用法,我只想要一个返回$$在Bash中传递的命令。

From python: 从python:

$ python
>>> import os
>>> os.getpid()
12252

$$不是特定于bash的 - 我相信它可以在所有符合POSIX标准的shell中使用,这相当于每个shell都不是故意的怪异。

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. 它假设一个具有正确形状的/proc的Linux, /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" . 作为替代方案,您可以获取字段6而不是字段4来获取“会话负责人”的PID。 Interactive shells apparently set themselves to be session leaders, and this id should remain the same across pipes and subshell invocations: 交互式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. 也就是说,这会引入对正在运行的shell的行为的依赖 - 它只有在它实际需要的PID时才设置会话ID。 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执行脚本而不是交互式脚本,这也不会在脚本中工作。

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: 将两者结合到一个答案中,提供两个选项,在POSIX-shell-required与no-POSIX-shell-required上下文之间进行权衡:

  1. POSIX shell available: use $$ POSIX shell可用:使用$$
  2. General cmdline: employ cut -d ' ' -f 4 /proc/self/stat 一般cmdline:使用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无关的选项。)

更少的角色并保证工作:

sh -c 'echo $PPID'

If you have access to the proc filesystem, then /proc/self is a symlink to the current /proc/$pid. 如果您可以访问proc文件系统,那么/ proc / self是当前/ proc / $ pid的符号链接。 You could read the pid out of, for instance, the first column of /proc/self/stat. 你可以从例如/ proc / self / stat的第一列读出pid。

If you are in python, you could use os.getpid(). 如果你在python中,你可以使用os.getpid()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python中以与平台无关的方式从路径中分离根 - splitting the root from path in a platform-agnostic way in python 如何以python版本不可知的方式检查http状态代码? - How to check http status code in python version agnostic way? 如何写入与操作系统无关的目录? - How to write to a directory os agnostic? 如何从Linux上运行的进程的命令行发出命令 - How to issue a command from the command line of a process running on Linux 使用Linux命令的Python脚本不能在Windows上运行。 有什么方法可以使它与操作系统无关吗? - Python script using Linux commands, does not run on Windows. Is there any way to make it OS agnostic? 如何在不使用索引的情况下创建目标语言不可知的可迭代SWIG C ++对象 - How to create a target language agnostic iterable SWIG C++ object without using the index 是否有与SqlAlchemy数据库无关的FROM_UNIXTIME()函数? - Is there a SqlAlchemy database agnostic FROM_UNIXTIME() function? 如何制作与索引无关的2元组? - How to make an index-agnostic 2-tuple? 如何在python中生成与操作系统无关的文件哈希? - How to generate OS agnostic file hash in python? 如何在漏勺中创建“类型不可知”SchemaNode - How to create a “type-agnostic” SchemaNode in colander
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM