PDA

View Full Version : change a python process title



RawMustard
February 12th, 2008, 05:06 AM
Is there no way to change the title of a python process?
I mean if I have 3 different python scripts running, they're all called python.

This makes it hard to kill ?which python????

Sorry if this is a dumb question, I'm not a seasoned programmer :(

ghostdog74
February 12th, 2008, 05:20 AM
one way, you can use os.getpid() from inside your Python script.


#!/usr/bin/python
import os
open("file","w").write(os.getpid())
#...remaining code


then from the command line, whenever you want to kill it


# kill $(cat file)

RawMustard
February 13th, 2008, 05:54 AM
Thanks. I know about that, but not what I was looking for.

PaulFr
February 13th, 2008, 06:14 AM
Does the following work for you ? It requires PyInline available at http://pyinline.sourceforge.net/
#!/usr/bin/env python

import dl
import PyInline
import time

libc = dl.open('/lib/libc.so.6')
if libc != 0 : libc.call('prctl', 15, 'fakename\0', 0, 0, 0)
else : print ('prctl not called')

m = PyInline.build(code=r"""
#include <Python.h>
#include <stdio.h>
#include <string.h>

void set_argv(char *str){
int argc;
char **argv;
Py_GetArgcArgv(&argc, &argv);
strncpy(argv[0], str , strlen(str));
memset(&argv[0][strlen(str)], '\0', strlen(&argv[0][strlen(str)]));
}
""", language="C")

m.set_argv('fakename')
while 1 :
print ('ha ha ha ')
time.sleep (1)1. The prctl call is for killall and top on Linux.
2. The m.set_argv call is for ps.

x0x
December 11th, 2008, 07:10 PM
http://code.google.com/p/procname/