return 0
return 0

Reputation: 4366

What's shell equivalent for killall if a python script is to be terminated?

So I have a binary file (binApp) to be killed

killall -9 binApp

Now, instead of using that binary file, I created another python script (pyBinapp.py) to execute the same task. I would like to kill that python task, will this work:

killall -9 pyBinApp

?

Also, what is that -9 mean? I couldn't find any article regarding this number.

Upvotes: 1

Views: 3508

Answers (4)

Alexandre Strube
Alexandre Strube

Reputation: 142

pkill does it directly, without the need of grepping it and piping it to kill.

pkill -f pyBinApp

Does exactly what you want.

Upvotes: 0

Roberto Medrano
Roberto Medrano

Reputation: 71

I use:

pgrep -f youAppFile.py | xargs kill -9

pgrep return the PID of the specific file and you kill only the specific application.

Upvotes: 0

Piotr Jurkiewicz
Piotr Jurkiewicz

Reputation: 1799

killall only matches the process name ("python" in your case), so it does not see script name ("pyBinApp.py") as it is one of python process arguments.

However pgrep can match against the full argument list with the -f flag, including the script file name.

If you're running python pyBinApp.py, try this to kill it:

kill `pgrep -f pyBinApp.py`

pgrep returns all PIDs matching the pattern and than kill is executed on all of them.

Upvotes: 3

Aidan Kane
Aidan Kane

Reputation: 4006

-9 is the signal to send (instead of the default SIGTERM) - SIGKILL. SIGTERM can be ignored by the application while SIGKILL cannot.

I don't thing it's safe to use killall in that case because you'd have to 'killall python' which may kill other things that are running. 'ps aux | grep python' to see what is running.

EDIT: Actually - I just tested it and so long as you're running pyBinapp.py directly (it's executable and contains #!) instead of passing it as an argument to python (eg python pyBinapp.py) you can kill it with killall pyBinapp.py

Upvotes: 2

Related Questions