26
votes

Is there a way to change the name of a process running a python script on Linux?

When I do a ps, all I get are "python" process names.

4

4 Answers

20
votes

http://code.google.com/p/procname/

Sample usage:

# Lets rename:    
>>> procname.setprocname('My super name')    

# Lets check. Press Ctrl+Z       
user@comp:~/procname$ ps

    PID TTY TIME CMD 

13016 pts/2 00:00:00 bash

13128 pts/2 00:00:00 My super name <-- it's here

It will only work on systems where prctl system call is present and supports PR_SET_NAME command.

20
votes

There is simplier (you don't need import any libs) but maybe not so elegant way. You have to do not use "env" inside the shebang line.

In other words, this will be named as "python" in process list:

#!/usr/bin/env python

But this will be named with your scriptname:

#!/usr/bin/python

So you'll be able to find it with something like pidof -x scriptname or ps -C scriptname

8
votes

the procname library didn't work for me on ubuntu. I went with setproctitle instead (pip install setproctitle). This is what gunicorn uses and it worked for me.

8
votes

There is the option of doing the following, though it only works on linux (with the prctl(2) call)

if sys.platform == 'linux2':
    import ctypes
    libc = ctypes.cdll.LoadLibrary('libc.so.6')
    libc.prctl(15, 'My Simple App', 0, 0, 0)