views:

964

answers:

5

Hello, this works, but it kills every Python process.

pkill python

However, I cannot do:

pkill myscript.py

I have also tried killall, but with no luck either. Do I have to user regular expressions?

By the way, I want to do this in a python script with import os.

A: 

you need to lookup the process id (pid). You can use the command "ps -A | grep NAME" and then apply "kill -9 PID". These commands can easily be translated to python.

Trying to use a "name" (as in pkill) can yield multiple matches and thus unexpected results (at least in the context set above in the question).

jldupont
how's that different from `pkill`?
hasen j
You don't NEED to use a pid. You can use whatever means of identifying the process that your process killer allows you to use. All *nix use a pid internally, but sometimes people want to kill all processes matching a pattern, not just a specific pid.
Michael Dillon
... but ultimately you get down to a pid: like I mentioned above, one has more change to kill the unintended process by going through some other means. Been there.
jldupont
You may want one of the ps variants that includes the argument data. E.g. "ps -AFww", "ps -A x", "ps ax", etc. I always liked "ps aux" or "ps alx". If you know the parent-pid (PPID) (the spawning process), there's "pstree -p $PPID" or "ps -Fww --ppid 4644". Awk can easily pull the PID out. E.g. kill \`ps -Fww | grep foo | awk '{print $2;}'\` But that is prone to killing the wrong processes by mistake...
Mr.Ree
+3  A: 

Are you able to have the process write it's pid to a file?
In Python you get the pid like this:

import os
os.getpid()

Killing by name is convenient, but sometimes has undesired consequences as you have seen.

gnibbler
I think he's trying to kill a specific python script from the (regular) command line, not do something from python
hasen j
It applies to both.I'd like to use the pkill myscript.py ...just to learn how to do it in command line
TIMEX
@hasen, still it is easier if you have a pid file, `kill 'cat pid'` using backticks instead of `'`
gnibbler
If he is writing the script, and he chooses a unique name for it, then he can easily guarantee that there will be only one match.
Michael Dillon
+1  A: 

Did you launch the Python subprocess from the same script you are killing it from? If so, see this question for details. If not, you can use pkill's -f option to search for the script name in the Python process's argument list, but you still run the risk of killing something you didn't intend to. See the man page for more info.

Ryan Bright
A: 

Try this:

echo '#!/usr/bin/env python' > myscript
cat myscript.py >>myscript
chmod +x myscript
./myscript

Of course you will have to change the code to kill a process named "myscript" On UNIX systems, an executable file contains a few bytes at the beginning which tell the OS what binary format is being used. If the first two bytes are #! then the OS assumes that this is actually a text file which can be executed by another program, and the OS loads the other program and passes to it, the text file.

In this case I probably could have written #!/usr/bin/python on the top line, but if your python is in /usr/local/bin, then it would not work. Instead, I leverage env to get it to search your normal path for python. All UNIX systems have env in /usr/bin. For a bit more info you can type in man env.

Michael Dillon
A: 

Thanks, that pkill helped in killing a python task specifically instead of killing all my python tasks as i had my script setup before.

Jacob