2

I am looking for an easy solution to change the title of a python process and the of individual python threads as displayed by top, htop or ps at runtime.

I want to avoid package dependencies but I do not have the requirement for the solution to be portable. It should run on a Gentoo system with Kernel version 4.4.38 and above.

I tried the following which unfortunately did not work on my system.

if sys.platform == 'linux2':
    import ctypes
    libc = ctypes.cdll.LoadLibrary('libc.so.6')
    libc.prctl(15, 'Another Name', 0, 0, 0)
Simon Fromme
  • 3,104
  • 18
  • 30
  • You need to change argv[0] of interpreter process, there is no simple and portable way to do it in pure python, some of examples were given in http://stackoverflow.com/questions/564695/is-there-a-way-to-change-effective-process-name-in-python – mucka Apr 12 '17 at 20:23

1 Answers1

0

Does it have to be within python? Can you run the script from bash as exec -a fancy_name python script.py?

If it needs to be from python but only needs to apply to new processes spawned from python, the following works for me:

import subprocess
subprocess.Popen(['cool_name','ls'], executable='watch')

Your approach (libc.prctl) in my case changes the process name as displayed by konsole's title but not as displayed in ps or top.

Luca Citi
  • 1,310
  • 9
  • 9
  • Thank's but I need to change the name at runtime from within python. Also I want to dynamically name threads depending on the task they are currently working on. – Simon Fromme Mar 08 '17 at 00:07