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!
Table of Contents
How to Kill multiple instances of a process
- Log into the shell of your Linux/Unix host
- 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!
Using pkill – A Single-Command Solution
The pkill
command is a versatile command-line utility in Unix-like operating systems, such as Linux, that allows users to send signals to processes. It’s primarily used to terminate processes based on different criteria like process name, process owner, or process group, among others. pkill
simplifies the process management task by eliminating the need to manually look up process IDs (PIDs) using commands like ps
, as you can directly reference processes by other identifiable attributes.
How to Install pkill
on Ubuntu, CentOS, Fedora, and RHEL Linux
The pkill
command comes pre-installed on most Unix-like operating systems, including Linux distributions like Ubuntu, CentOS, and Debian.
If you find that the pkill
command is missing, it’s part of the procps
or procps-ng
package. You can install this package using the package manager specific to your Linux distribution:
- For Ubuntu, Debian, and other Debian-based distributions, use
apt
:bash sudo apt-get update sudo apt-get install procps
- For CentOS, RHEL, Fedora, and other Red Hat-based distributions, use
yum
ordnf
:bash sudo yum install procps-ng
orbash sudo dnf install procps-ng
After this installation, you should have access to thepkill
command. Remember to usesudo
if you’re not logged in as the root user.
How to force kill running processes using pkill
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