Linux: How to Kill all instances of a script/program by using a string search
This article will show you how to kill (or end) multiple processes or instances of a running script with a single command.
If you are like me, you have run into this situation many many times. You run the ps command to list all the currently running processes and you see that there are multiple instances running of a certain program. You want to end all these instances but do not want to run the kill command for each one of those specifying the pid manually. Killing all instances of this program is very simple and I’ll show you how to do it using a 1 line command!
How to Kill multiple instances of a process
- Log into the shell of your Linux/Unix box
- If you do not know the name of the script you will like to kill, run:
ps -ef
This will show you a list of all the currently running processes. In the right-most column, you will find the path and filename of the script you want to end. - To kill all instances of this script, run the command:
ps -ef | grep [search string] | awk '{print $2}' | xargs kill -9
Be sure you substitute [search string] to your own value. - Now run ps –ef again and all running instances of that script will be gone!
Alternative Method
If your Linux distribution has pkill installed, simply run the command:
pkill -9 -f [search string]
Ever heard about the “killall” command??
i’ve heard about that, i’ve also heard that sometimes it is not installed…
For some reason killall doesn`t work in script, only in console at my android phone. pkill performs fine everywhere