Skip to content

Instantly share code, notes, and snippets.

@jpcaparas
Created May 23, 2019 04:54
Show Gist options
  • Save jpcaparas/c7decbd651af627fff61596e8c100a8e to your computer and use it in GitHub Desktop.
Save jpcaparas/c7decbd651af627fff61596e8c100a8e to your computer and use it in GitHub Desktop.
Kill proceses by search
#!/bin/bash
set -e
searchterm=$1
verbose=false
usage() {
echo "Usage: [-v verbose] searchterm"
exit 1
}
[[ -z $searchterm ]] && usage;
while getopts ":v:" o; do
case "${o}" in
v)
verbose=true
;;
esac
done
# The leading "/" prevents capturing the grep process
procids=$(ps aux | grep -i "[/]${searchterm}" | awk '{ print $2 }')
if [[ -z $procids ]]; then
[[ $verbose = true ]] && printf "No matches found."
exit 0
fi
[[ $verbose = true ]] && printf "Killing processes with ID/s:\n${procids}\n";
kill -9 $procids
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment