glogglog

Change process name in Python

Linux:

libc6's prctl function with option PR_SET_NAME
       PR_SET_NAME (since Linux 2.6.9)
              Set the process name for the calling process, using the value in
              the location pointed to by (char *) arg2.  The name can be up to
              16  bytes  long,  and  should  be null terminated if it contains
              fewer bytes.

code is

import ctypes
libc = ctypes.CDLL('libc.so.6')
libc.prctl(15, '%s\0' %procname, 0, 0, 0)

---------------------------------------------------------------------------------------

BSD:

import dl
libc = dl.open('/lib/libc.so.6')
libc.call('setproctitle', '%s\0' %procname)

---------------------------------------------------------------------------------------
a script to show value of MACRO

echo -e "#include <sys/prctl.h>\n#include <stdio.h>\nint main(){printf(\"%d\\\n\",PR_SET_NAME);return 0;}" |gcc -xc - -o /tmp/test && /tmp/test



Tags: python |


1 comments:
#1: A module for doing it in a portable way
By: daniele.varrazzo | 2010-04-27 14:19

This uses prctl, setproctitle, argv clobber and whatever needed on a number of platforms. http://code.google.com/p/py-setproctitle/ Cheers!


sevenever@gmail.com
Copyright © 2008. All rights reserved.