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

Question:

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.

Asked By: Johnny Utahh

||

Answers:

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

Answered By: stew

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.

Answered By: millimoose

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

Answered By: user149341

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

Answered By: Johnny Utahh

From python:

$ python
>>> import os
>>> os.getpid()
12252
Answered By: Aleksandr Levchuk

Fewer characters and guaranteed to work:

sh -c 'echo $PPID'
Answered By: Jordan Samuels