Changing the Process Name of a Python Script

changing the process name of a python script

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.

Is there a way to change effective process name in Python?

Simply put, there's no portable way. You'll have to test for the system and use the preferred method for that system.

Further, I'm confused about what you mean by process names on Windows.

Do you mean a service name? I presume so, because nothing else really makes any sense (at least to my non-Windows using brain).

If so, you need to use Tim Golden's WMI interface and call the .Change method on the service... at least according to his tutorial.

For Linux none of the methods I found worked except for this poorly packaged module that sets argv[0] for you.

I don't even know if this will work on BSD variants (which does have a setproctitle system call). I'm pretty sure argv[0] won't work on Solaris.

Change process name of Python script

There's no nice way that I've found to change the name of a running process in Windows, but you can create small .exe stubs with ExeMaker rather than resorting to py2exe packaging or copying the interpreter

The exe stub uses its own module name to calculate the .py script invoked. You should be able to use an exe resource editor to change the icon.

Change process name while executing a python script

You can use pure ctypes calls:

import ctypes

lib = ctypes.cdll.LoadLibrary(None)
prctl = lib.prctl
prctl.restype = ctypes.c_int
prctl.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_ulong,
ctypes.c_ulong, ctypes.c_ulong]

def set_proctitle(new_title):
result = prctl(15, new_title, 0, 0, 0)
if result != 0:
raise OSError("prctl result: %d" % result)

Compare:

  • http://man7.org/linux/man-pages/man2/prctl.2.html
  • http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/prctl.h#L53


Related Topics



Leave a reply



Submit