Friday, June 7, 2019

Bash - kill everything with a matching name

Just a small script for killing matching processes by name:

#!/bin/bash

if [ -z $1 ]
then
    printf "keyword is empty\n\n"
    exit
fi

let k=0
let min=1

# mind the double brakets in do part
for i in $(ps -e | grep "$1"); do ((k++)); done

if [ "${k}" -lt "${min}" ]
then
    printf "no such process to kill\n\n"
    exit
fi


for i in $(ps -e | grep $1 | cut -s -f1 -d\t); do kill -9 ${i}; done

No comments:

Post a Comment