how to kill a python process?

Donn Cave donn at u.washington.edu
Mon Feb 6 13:08:37 EST 2006


In article <1139170910.513664.169900 at o13g2000cwo.googlegroups.com>,
 "MackS" <mackstevenson at hotmail.com> wrote:

>  I just read that on some systems perl allows you to rename the
> process by assigning to $0:
> 
> http://www.linuxquestions.org/questions/showthread.php?t=401299
> 
> Is there a way to do the same in python? My trouble is that this are
> small utilities that I start from the command line and later need to
> stop; I just find myself unable to do so with stopping all of them...

Someone has undoubtedly managed to do this, with a C module.
It isn't portable - there isn't a standard (e.g., POSIX) way
to do it - and no doubt that's one reason you won't find it in
standard Python.

If I ever needed something like that (I haven't), I guess I
would use execve() to start the process with a name other
than python.  E.g.,

   os.execve('/usr/bin/python', ['test1', '/tmp/test1'], os.environ)

   $ ps wwaux | fgrep test1
   donn       227 ...   1568  p1  S     9:30AM   0:00.19 test1 /tmp/test1

(instead of normal)

   os.execve('/usr/bin/python', ['python', '/tmp/test1'], os.environ)

You can make a cover program to do this:

   os.execve('/usr/bin/python', [sys.argv[1] + sys.argv[1:], os.environ)


   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list