6

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

1 Answer 1

3

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

2
  • So why does python sometimes show up as python and other times as the script name? And why does the script name sometimes show up as just the basename and other times as the full path?
    – dg99
    Commented Apr 28, 2016 at 18:01
  • Application itself can change name: stackoverflow.com/a/33912448/3479860 Commented Apr 28, 2016 at 18:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .