Linux – How does Linux decide the /proc/PID/stat “name” of a process

linuxproc

Currently my machine has 5 processes running scripts through python, but Linux thinks only 2 of them have the name python (according to /proc/$pid/stat). That is, pgrep -af python shows:

1784 /usr/bin/python -Es /usr/sbin/foo
2306 /usr/bin/python /usr/bin/bar
16964 /usr/bin/python /usr/bin/terminator --geometry=1400x1000
24137 python /home/me/bin/baz.py --arg 70000
25760 python2 -m guake.main

whereas pgrep -a python shows only:

24137 python /home/me/bin/baz.py --arg 70000
25760 python2 -m guake.main

Here are the names that Linux has given to these processes:

% for pid in $(pgrep -f python); do cut -d' ' -f2 /proc/$pid/stat; done
(foo)
(bar)
(/usr/bin/termin)
(python)
(python2)

So how does Linux decide whether python or the script name will be the process name? And why do foo and bar become the name when the terminator process gets the full path instead?

I assume the manner of invocation matters. I don't know how these three programs were invoked, but here are their shebangs:

/usr/sbin/foo: #!/usr/bin/python -Es
/usr/bin/bar: #!/usr/bin/python
/usr/bin/terminator: #!/usr/bin/python

This one was definitely invoked using the shebang:

/home/me/bin/baz.py: #!/usr/bin/env python

And Guake is launched from a Bash script like so:

exec /usr/bin/env python2 -m guake.main "$@" </dev/null >/dev/null 2>&1 &

My naive guess is that /usr/bin/env causes the word following it to become the process name, but I assume there's more to it than just that. (And even if that is the case, how does it assign that name?)

Best Answer

This is down to a Linux:

When a program starts another, it should use the name of the executable file as command line parameter $0, but it may choose to do otherwise. The Name field of /proc/PID/status is always set to the name of the executable by the kernel (but truncated to 15 characters).

The application itself can change a name. You can get the longer name from /proc/PID/cmdline (read up to the first null byte).

Related Question