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.
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
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.
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.
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)