how to kill a python process?

M

MackS

Hello!

This question does not concern programming in python, but how to manage
python processes. Is there a way to "name" a python process? At least
on Linux, if I have two python programs running, they both run under
the name "python"

#pidof program1.py
[empty line]
#pidof program1.py
[empty line]
# pidof python
1249 854

Is there a way to make them run under their own names, e.g., to make it
easier to kill them by name using killall? Or am I stuck with
registering the pid of each python process I create and then refer to
that list whenever I need to selectively stop one of them? The latter
makes managing a long list of processes very cumbersome...

Thanks for any help!

Mack
 
D

Diez B. Roggisch

MackS said:
Hello!

This question does not concern programming in python, but how to manage
python processes. Is there a way to "name" a python process? At least
on Linux, if I have two python programs running, they both run under
the name "python"

#pidof program1.py
[empty line]
#pidof program1.py
[empty line]
# pidof python
1249 854

Is there a way to make them run under their own names, e.g., to make it
easier to kill them by name using killall? Or am I stuck with
registering the pid of each python process I create and then refer to
that list whenever I need to selectively stop one of them? The latter
makes managing a long list of processes very cumbersome...

Yes, you are. I'm not sure how to rename processes - but _if_ it kan be
done, it means that somebody else could maybe do it, too - and in case
of the same name is accidentially killed.

So - stick with the PID- After all, it is not _so_ cumbersome to keep a
reference to e.g. popen-objects.

Diez
 
J

Jorgen Grahn

Hello!

This question does not concern programming in python, but how to manage
python processes. Is there a way to "name" a python process? At least
on Linux, if I have two python programs running, they both run under
the name "python" ....
Is there a way to make them run under their own names, e.g., to make it
easier to kill them by name using killall?

Funny, I had to experiment a bit to see how it worked. This is surely a FAQ,
because it's important. IMHO, the interpreter for a Unix program should be
invisible, so that the program behaves like as a first-class process in
every way.

$ python foo1.py

This one gets called 'python', as expected.

$ head -1 foo2.py
#!/usr/bin/env python
$ ./foo2.py

This one gets called 'python', too. I've been using this version because I
read somewhere it was the recommended idiom -- but I will stop doing it now
that I know it mangles the process name.

$ head -1 foo3.py
#!/usr/bin/python
$ ./foo3.py

This is the traditional shebang form used for shell and Perl scripts,
and it names the process 'foo3.py' so you can killall(1) it nicely.
Or am I stuck with
registering the pid of each python process I create and then refer to
that list whenever I need to selectively stop one of them?

If a program starts another one and needs to kill it, it /should/ remember
its pid. You don't want programs which cannot live together on one machine
without killing each other's children.

But as I wrote above, you /do/ want to have meaningful information in
top(1), ps(1) and so on. Having half a dozen processes named python (or
java, or whatever) and wanting to kill one specific is not my idea of fun
....

/Jorgen
 
M

MackS

Hi Jorgen

You wrote that:
$ head -1 foo3.py
#!/usr/bin/python
$ ./foo3.py

This is the traditional shebang form used for shell and Perl scripts,
and it names the process 'foo3.py' so you can killall(1) it nicely.

It doesn't work on my system; I just get yet another process called
python. 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...

Cheers

Mack
 
D

Donn Cave

"MackS said:
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, (e-mail address removed)
 
J

Jorgen Grahn

Hi Jorgen

You wrote that:


It doesn't work on my system; I just get yet another process called
python.

Strange ... the other shebang-able programs (interpreters) I run (/bin/sh,
perl) work like I described. That's something I expect to be standard
operating procedure on any Unix (although I haven't spent much time outside
Linux recently).

Surely the interpreter in question doesn't have to do any magic to make it
work?
I just read that on some systems perl allows you to rename the
process by assigning to $0:

You shouldn't have to do that. I'm pretty sure.

/Jorgen
 
D

Donn Cave

It doesn't work on my system; I just get yet another process called
python.

Strange ... the other shebang-able programs (interpreters) I run (/bin/sh,
perl) work like I described. That's something I expect to be standard
operating procedure on any Unix (although I haven't spent much time outside
Linux recently).[/QUOTE]

Does it depend on the question? If you have been checking
this with "ps", try "ps -f". There seem to be a couple of
different versions of the command line, on Linux.

Donn Cave, (e-mail address removed)
 
M

MackS

Hi Jorgen

THanks for your help. I began writing a wrapper around python did (as
Donn suggested), but then noticed that this was due to not having
educated myself on the options you can pass to ps, pidof and top:

running pidof -x and using the 'c' command in top work pretty nicely.
That way I no longer see 23 'pythons' in the system : ) and know which
one to stop.

Mack
 
S

Steve Horsley

MackS said:
Hello!

This question does not concern programming in python, but how to manage
python processes. Is there a way to "name" a python process? At least
on Linux, if I have two python programs running, they both run under
the name "python"

#pidof program1.py
[empty line]
#pidof program1.py
[empty line]
# pidof python
1249 854

Is there a way to make them run under their own names, e.g., to make it
easier to kill them by name using killall? Or am I stuck with
registering the pid of each python process I create and then refer to
that list whenever I need to selectively stop one of them? The latter
makes managing a long list of processes very cumbersome...

Thanks for any help!

Mack

I ran a script called test, and found this in my process list
(sorry, the emailer wraps it):

steve 14513 14452 0 21:43 pts/2 00:00:00 /usr/bin/python
../test

But I also find that "killall test" kills it. So you don't need
"killall python" at all.

HTH
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top