Skip to content

Instantly share code, notes, and snippets.

@jay16
Created May 4, 2014 11:20
Show Gist options
  • Save jay16/ea2463f474ebbc9dea36 to your computer and use it in GitHub Desktop.
Save jay16/ea2463f474ebbc9dea36 to your computer and use it in GitHub Desktop.
#帮助说明
help() {
echo "usage:kp keyword username"
echo " eg:kp resque webmail"
exit 0
}
#第一个参数为空,提示帮助说明
[ -z $1 ] && help
keyword=$1
user=$2
#第二个参数为空,设置默认用户webmail
[ -z $2 ] && user="webmail"
echo "keyword:${keyword} - user:${user}"
id_str=$(ps axuw | grep ${keyword} | \
grep -v grep | \
awk -v awk_user="${user}" 'BEGIN { -F" "} \
{if($1==awk_user) printf("%s ",$2);} \
END { }')
id_ary=(${id_str})
id_num=${#id_ary[@]}
echo "find ${id_num} result."
#查找进程数量为0时直接退出
[ ${id_num} -eq 0 ] && exit 0
ps axuw | grep ${keyword} | grep -v grep | awk -v awk_user="${user}" 'BEGIN{ -F" "} { if($1==awk_user) printf("%s\n ", $0);}'
echo -n "kill the above ${id_num} process?(y/n)"
read yn
case ${yn} in
Y|y)
kill -9 ${id_str}
echo "kill over!"
;;
N|n)
echo "do nothing"
;;
*)
echo "do nothing"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment