Algorithms, Blockchain and Cloud

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.

#!/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:

# 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) —

176 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 (AMP Version)

Exit mobile version