Skip to content

Instantly share code, notes, and snippets.

@jobwat
Last active September 10, 2021 06:13
Show Gist options
  • Save jobwat/e99c446581edaef9e6a2 to your computer and use it in GitHub Desktop.
Save jobwat/e99c446581edaef9e6a2 to your computer and use it in GitHub Desktop.
My kill9 script (old)
#!/bin/bash
# ---------------------------------------------------
# Original script name: kill9
#
# Object: Kill the matching given name processes
# ---------------------------------------------------
if [ -z $1 ]; then
echo "Error: missing search arguments... exiting"
exit 1
fi
# Option(s) of the kill command (signal)
kill_option="-9"
# transform params into regexps
chained_regexp=''
any_term_regexp=''
for (( i=1; i<$#; i++ )); do
chained_regexp+="${!i}.*"
any_term_regexp+="${!i}|"
done
chained_regexp+="${*: -1}"
any_term_regexp+="${*: -1}"
# Get PIDs of process matching given parameter
pids=`ps -Af | grep -Ev "($0|grep -)" | grep -i "$chained_regexp" | awk '{ print $2 }' | tr '\n' ' '`
if [ -z "$pids" ]; then
echo "Nothing to kill like that...."
else
echo ""
# a bit of display with a bit of color :D
ps -f -p $pids | sed -r "s/($any_term_regexp)/$(tput sgr0)$(tput bold)$(tput setaf 1)\1$(tput sgr0)/g"
echo ""
echo "Kill those Y/N ??"
read kill
if [[ $kill == Y || $kill == y || $kill == O || $kill == o ]]; then
echo "Killing of $pids"
sudo kill $kill_option $pids
else
echo "Nothing killed."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment