A Simple BASH Script to Search and Kill Processes By Name


At Linux, sometimes we want to search all the processes that contain some name and then terminate all of them. We can use “ps augx” to list all processes and the locate those with keyword by using “grep keyword” command. Next, we need to split each line by whitespace delimiter and list the second column which is the process ID. Finally, we feed into xargs command to kill them.

Here is the little BASH Script that does the job well.

1
2
3
4
5
6
7
8
#!/bin/bash
 
if [ "$1" == "" ]; then
    echo "Usage: $0 process-name"
    exit 1
fi
 
ps augx | grep "$1" | awk '{print $2}' | xargs kill
#!/bin/bash

if [ "$1" == "" ]; then
    echo "Usage: $0 process-name"
    exit 1
fi

ps augx | grep "$1" | awk '{print $2}' | xargs kill

Name this “kill-all” and the usage would be like:

1
2
# kill all running processes with keyword "python" in the process command path
$./kill-all python
# kill all running processes with keyword "python" in the process command path
$./kill-all python

Use this at your risk.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
193 words
Last Post: Teaching Kids Programming - Greedy Algorithm to Complete Tasks
Next Post: PHP Function to Check if a String is a Valid Domain Name via Regex

The Permanent URL is: A Simple BASH Script to Search and Kill Processes By Name

One Response

  1. GishQj

Leave a Reply