A trick to rename python process in output of 'top' and 'ps'.


set_argv.c:

#include
#include
#include

int get_argc(){
int argc;
char **argv;
Py_GetArgcArgv(&argc;, &argv;);
return argc;
}

void set_argv_via_number(int num,char *str){
int argc;
char **argv;
Py_GetArgcArgv(&argc;, &argv;);
strncpy(argv[num], str , strlen(str));
memset(&argv;[num][strlen(str)], '\0', strlen(&argv;[num][strlen(str)]));
}


set_processname.pyx:

#Warning: this module is not safe and may involve some dirty hacks.
import sys,dl,glob
cdef extern int get_argc()
cdef extern void set_argv_via_number(int num,char *buf)
def set_argv0(thename):
set_argv_via_number(0,thename)

def set_argv(thename):
argc = get_argc()
print argc
for i in xrange(argc+1):
if i==0:
set_argv0(thename)
else:
pass
def set_name(thename):
if sys.platform == 'linux2':
# Set process name. Only works on Linux >= 2.1.57.
try:
libc_loc = glob.glob("/lib/libc.so*")[0]
libc = dl.open(libc_loc)
libc.call('prctl', 15, thename, 0, 0, 0) # 15 is PR_SET_NAME
except:
pass

def set_processname(thename):
space_padding_num = len(sys.argv[0])-len(thename)
if space_padding_num < 0:
#thename is longer than original name
newname = thename[:len(sys.argv[0])]
else:
#thename is shorter. add padding.
newname = thename + " " * space_padding_num

set_name(newname)
set_argv(newname)

compile.sh:
#!/bin/sh
CC=mipsel-linux-gcc

pyrexc set_processname.pyx
$CC -c -fPIC -I/usr/include/python2.5/ set_argv.c
$CC -c -fPIC -I/usr/include/python2.5/ set_processname.c
$CC -shared set_argv.o set_processname.o -o set_processname.so