Davyd ([info]davyd) wrote,
@ 2006-02-10 11:10:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Changing process name in Python
Following on from Aaron, I implemented the same thing in Python for Deskbar.

Some people may have misunderstood what Aaron was telling them. The prctl() call doesn't change your entry in the process listing (you need to fiddle with your argv to do that - have not worked out how to do this in Python yet); prctl() only allows you to change the name that you use with killall.

Still searching for the complete solution though.


(Post a new comment)

I want
(Anonymous)
2006-02-10 04:57 am UTC (link)
I'd really like this as well inside of Python. I can deal with the prctl part (for completeness), but requiring the help of an external bash script isn't always easy. I'd like to find a way to make this work, even if it is Linux 2.6 specific.

(Reply to this)


[info]jerub
2006-02-10 06:27 am UTC (link)
I patched proctitle to do this this morning, after i saw the prctl thing.

proctitle 0.0.2
prctl patch

Note! no error is raised if you run it on a version of the kernel that doesn't support it!

(Reply to this) (Thread)


[info]davyd
2006-02-10 06:40 am UTC (link)
So proctitle is a piece of Python to set the process title?

(Reply to this) (Parent)(Thread)


[info]jerub
2006-02-10 06:48 am UTC (link)
Yep. alters sys.argv for you. The patch makes it do the prctl thing as well.

(Reply to this) (Parent)(Thread)


[info]davyd
2006-02-10 07:16 am UTC (link)
This seems remarkably evil, however very similar to something I was thinking of earlier today. It wouldn't be portable to a number of Unixes.

(Reply to this) (Parent)


(Anonymous)
2006-02-10 08:14 pm UTC (link)
I needed to change from #include <linux/prctl.h> to #include <sys/prctl.h>,
otherwise I got a "implicit declaration of function `prctl`".

The difference being:
sys-kernel/linux-headers-2.6.11-r3 (/usr/include/linux/prctl.h)
sys-libs/glibc-2.3.6-r2 (/usr/include/sys/prctl.h)

(Reply to this) (Parent)

env
[info]pixelbeat
2006-02-10 12:28 pm UTC (link)
Doesn't that all happen automatically if you don't use env on the first line?

(Reply to this)

dl / ctypes
[info]jdahlin
2006-04-06 06:08 pm UTC (link)
Two python only solutions, just as a reference:

dl solution:

import dl
libc = dl.open('/lib/libc.so.6')
libc.call('prctl', 15, 'snake warrior\0', 0, 0, 0)

ctypes solution:

import ctypes
libc = ctypes.CDLL('libc.so.6')
libc.prctl(15, 'snake warrior', 0, 0, 0)

dl is unix only, ctypes is portable but not in the standard library until python 2.5

(Reply to this) (Thread)

Re: dl / ctypes
(Anonymous)
2006-05-27 11:53 am UTC (link)
Thanks for this code with libc and dl module. This was really useful!!
Could be also in python guides or so, .. I was fucking my brain with proctitle module, that never worked for me ...

Thank you again!!!!!!

(Reply to this) (Parent)

Re: dl / ctypes
(Anonymous)
2007-02-05 04:57 pm UTC (link)
I found on FreeBSD i need to use libc.setproctitle('snake warrior') ...

(Reply to this) (Parent)(Thread)

Re: dl / ctypes
(Anonymous)
2007-04-30 08:01 pm UTC (link)
The dl method in FreeBSD

import dl

libc = dl.open('/lib/libc.so.6')
libc.call('setproctitle', 'hippy\0')

(Reply to this) (Parent)

Re: dl / ctypes
(Anonymous)
2008-06-24 04:03 pm UTC (link)
Hello!

Thank's for the solution. But I meet a trouble: dl isn't standard python module in 64-bit Debian.
I've written a little python library procname for setting and getting process names in Python. It's written in pure C and can be compiled on any linux with make and gcc. Just install python headers.
It could be get here: http://code.google.com/p/procname/
Just download, ungzip and make. You'll get a procname.so file - place it in any PYTHONPATH dir (or just current dir of you program)

Usage is extremally simple:
>>> import procname
>>> procname.getprocname()
'python'
>>> procname.setprocname('My super-name 9')

This affects both ps and top output.

Library works stable (I use it in my multiprocess python daemon).

(Reply to this) (Parent)(Thread)

Re: dl / ctypes
(Anonymous)
2008-06-24 04:05 pm UTC (link)
Download link: http://code.google.com/p/procname/downloads/list
Just 904 bytes!

(Reply to this) (Parent)


Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…