简体   繁体   中英

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?

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.

From 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 :

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.

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:

$ 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.

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:

  1. POSIX shell available: use $$
  2. General cmdline: employ 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.)

更少的角色并保证工作:

sh -c 'echo $PPID'

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().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM