Linux: How to Kill all instances of a script/program by using a string search

kill processes linux, stop script execution

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

  1. Log into the shell of your Linux/Unix host
  2. 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.
  3. 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.
  4. 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.

See also  5 Ways to Pass Command-Line Parameters to a Linux Shell Script

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:

  1. For Ubuntu, Debian, and other Debian-based distributions, use apt:
    bash sudo apt-get update sudo apt-get install procps
  2. For CentOS, RHEL, Fedora, and other Red Hat-based distributions, use yum or dnf:
    bash sudo yum install procps-ng
    bash sudo dnf install procps-ng
    After this installation, you should have access to the pkill command. Remember to use sudo if you’re not logged in as the root user.

How to force kill running processes using pkill

pkill -9 -f [search string]

3 thoughts on “Linux: How to Kill all instances of a script/program by using a string search”

  1. For some reason killall doesn`t work in script, only in console at my android phone. pkill performs fine everywhere

Leave a Comment