Is there a way to change the name of an efficient process in Python?

Is it possible to change the name of an executable process to a Python script? I want to show a different name instead of the real process name when I get a list of system processes. In C, I can install

strcpy(argv[0],"othername"); 

But in Python

 argv[0] = "othername" 

doesn't seem to work. When I get a list of processes (with ps ax in my linux box), the real name does not change. I prefer a portable solution (or another solution for posix and another for windowed environments) if one exists.

Thank you in advance

+58
python process arguments ps hide
Feb 19 '09 at 10:37
source share
8 answers

Simply put, there is no portable way. You will need to check the system and use the preferred method for this system.

Also, I am confused by what you mean by process names in Windows.

Do you mean the name of the service? I think so because nothing else makes any sense (at least for my non-Windows using the brain).

If so, you need to use the Tim Golden WMI interface and call the .Change method for the service ... at least according to its tutorial .

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

I don’t even know if this will work on BSD variants (which have a setproctitle system call). I am sure argv [0] will not work on Solaris.

+10
Feb 19 '09 at 12:26
source share

I recently wrote a Python module to change the name of a process in a portable way: check https://github.com/dvarrazzo/py-setproctitle

This is a wrapper around the code used by PostgreSQL to perform name changes. It is currently tested on Linux and Mac OS X: Windows (with limited functionality) and BSD ports are on the way.

Edit: as of July 2010, the module works with BSD and has limited functionality on Windows and ported to Python 3.x.

+65
Dec 08 '09 at 12:40
source share

in fact you need 2 things on linux: change argv[0] from C (for ps auxf and friends) and call prctl with the flag PR_SET_NAME .

There is absolutely no way to make the first part of python itself. Although you can just change the process name by calling prctl.

 def set_proc_name(newname): from ctypes import cdll, byref, create_string_buffer libc = cdll.LoadLibrary('libc.so.6') buff = create_string_buffer(len(newname)+1) buff.value = newname libc.prctl(15, byref(buff), 0, 0, 0) def get_proc_name(): from ctypes import cdll, byref, create_string_buffer libc = cdll.LoadLibrary('libc.so.6') buff = create_string_buffer(128) # 16 == PR_GET_NAME from <linux/prctl.h> libc.prctl(16, byref(buff), 0, 0, 0) return buff.value import sys # sys.argv[0] == 'python' # outputs 'python' get_proc_name() set_proc_name('testing yeah') # outputs 'testing yeah' get_proc_name() 

ps auxf will only show "python" after this :( But top and ps -A will show the new process name "testing yes" :). Also killall and pkill will work with the new name.

btw, procname from googlecode also changes argv[0] , thus even changing the output of ps auxf .

UPDATE The solution posted in this answer sometimes does not play in FreeBSD. I now use the py-setproctitle indicated in this answer for the year or both on different linux and freebsd. Until it worked! Everyone needs it too! :). It uses almost the same code that PostgreSQL uses in its main database and child processes.

+45
May 28 '09 at 20:39
source share

Firstly, I'm not sure that just setting argv[0] in a C program will change the name displayed in ps . Perhaps this happens on some unixen, but I understand that this is not expected.

Secondly, since Windows is not specifically compatible with POSIX, only a few things are "portable" between POSIX and non-POSIX. Since you specifically say “ps”, I assume POSIX is your priority and Windows may not work.

More importantly, my understanding of the argv[0] change is that exec calls are required to make these changes. In particular, the call to exec has both a path to the executable file and a separate argv list. Making your own call allows you to break the shell convention of sending an executable name to argv[0] .

You have an OS library process control that gives you direct access to the OS library for this. You should consider hacking the script into two parts - the starter and the “real job”. The starter sets up the runtime and does the real work with the required parameters.

In C, you replace your own process with another. In Python, you replace the old Python interpreter with a new one that has another argv [0]. I hope this will not happen. Some programs check argv [0] to decide what they are doing.

You also have subprocess.popen , which you can use to set the desired arguments and executables. In this case, however, the parental process must be delayed in order to assemble the child when the child is finished. Parent can do nothing more than Popen.wait

+4
Feb 19 '09 at 11:31
source share

Take a look at the setproctitle package

This is a fairly portable version and works on many platforms.

+4
Jan 05 '10 at 14:28
source share

My answer to a similar question is marked as duplicate :

Easier (you don’t need to import any libraries), but maybe not so elegant. You should not use "env" inside the shebang line.

In other words, this will be called "python" in the process list:

 #!/usr/bin/env python 

But this will be indicated with your script name:

 #!/usr/bin/python 

So you can find it with something like pidof -x scriptname or ps -C scriptname

+2
Nov 25 '15 at 9:12
source share

I found python-prctl to work fine under Linux. You will need to find something else for Windows.

0
Feb 07 '11 at 3:45 a.m.
source share
 In [1]: import sys In [2]: print sys.argv[0] C:\Python25\scripts\ipython.py In [3]: sys.argv[0] = 'foo' In [4]: print sys.argv[0] foo 

Note the single "="

-8
Feb 19 '09 at 10:39
source share



All Articles