28

Is there a way to change the name of a process running a python script on Linux?

When I do a ps, all I get are "python" process names.

jldupont
  • 93,734
  • 56
  • 203
  • 318

4 Answers4

23

There is simplier (you don't need import any libs) but maybe not so elegant way. You have to do not use "env" inside the shebang line.

In other words, this will be named as "python" in process list:

#!/usr/bin/env python

But this will be named with your scriptname:

#!/usr/bin/python

So you'll be able to find it with something like pidof -x scriptname or ps -C scriptname

Sasha Ru
  • 625
  • 1
  • 6
  • 16
  • 2
    This is exactly what I am looking for. Thank you – user1061392 Jun 02 '15 at 23:01
  • Effective when we are working with bash files. Thank you. – dlopezgonzalez Nov 04 '15 at 13:35
  • Although that does defeat the purpose of using env. – Christopher Barber Jun 18 '19 at 19:27
  • 2
    @ChristopherBarber The "purpose" of env is [debatable](https://unix.stackexchange.com/questions/29608/why-is-it-better-to-use-usr-bin-env-name-instead-of-path-to-name-as-my/29620#29620) and generally not well understood by most people using it, anyway. The way I see it, this is just one more reason to _avoid_ prefixing with env. – FeRD Jul 21 '19 at 06:47
  • this doesnt seem to work on centos 7, any idea why? I can still see python scriptname, even when I run from shell just use ./scriptname.py – tesla1060 Aug 26 '19 at 09:50
  • @tesla1060, it works for me on centos 7. It should not depend on how you're launching script; please show your shebang line. – Sasha Ru Sep 13 '19 at 21:38
  • @SashaRu my shebang line:#!/usr/bin/env python, when running, it shows as python ./test.py in htop, the script is named test.py – tesla1060 Sep 16 '19 at 05:53
  • @tesla1060 that is expected with using env. Try to change shebang to #!/usr/bin/python – Sasha Ru Sep 17 '19 at 21:26
  • @SashaRu still not right, i am getting /usr/bin/python ./test.py on my htop – tesla1060 Sep 18 '19 at 02:32
  • @tesla1060, this question is about process cmd name in linux (man ps). Your question is about htop. – Sasha Ru Sep 19 '19 at 23:10
21

http://code.google.com/p/procname/

Sample usage:

# Lets rename:    
>>> procname.setprocname('My super name')    

# Lets check. Press Ctrl+Z       
user@comp:~/procname$ ps

    PID TTY TIME CMD 

13016 pts/2 00:00:00 bash

13128 pts/2 00:00:00 My super name <-- it's here

It will only work on systems where prctl system call is present and supports PR_SET_NAME command.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
9

the procname library didn't work for me on ubuntu. I went with setproctitle instead (pip install setproctitle). This is what gunicorn uses and it worked for me.

amwinter
  • 3,121
  • 2
  • 27
  • 25
8

There is the option of doing the following, though it only works on linux (with the prctl(2) call)

if sys.platform == 'linux2':
    import ctypes
    libc = ctypes.cdll.LoadLibrary('libc.so.6')
    libc.prctl(15, 'My Simple App', 0, 0, 0)
bufh
  • 3,153
  • 31
  • 36
Matt Seymour
  • 8,880
  • 7
  • 60
  • 101
  • Doesn't work Debian 6, x86_64. – Hongli Mar 07 '13 at 13:08
  • neither works on ubuntu :( – Paolo Casciello Jul 25 '13 at 14:35
  • Try replacing 16 with PR_SET_NAME – Dan Jul 25 '13 at 16:24
  • Successfully tested with Python 2.7 on Ubuntu 12.04 / Linux 3.1.10 x86_64. **Note:** it *does* change the name as seen in `ps` but not the cmdline as returned by `ps w`. – bufh Apr 16 '15 at 08:47
  • Python 3 (semicolons to denote lines because no multi-line on SO) ``` libc.prctl(15, b"New Name", None, None, None); # And to retrieve: (I don't know if you have to initialize c_char_p with more than a single byte, but it seems more correct to allocate the correct amount of memory first); proc_name = ctypes.c_char_p(b"\0" * 16); libc.prctl(16, proc_name, None, None, None); print(proc_name.value); ``` – Skinner927 Sep 20 '19 at 18:11