根据进程包含的文本搜索并kill进程
APPPID=ps aux|grep "$1"|grep -v "grep"|grep -v "$0"|head -n 1|cut -c 9-15
if [ $APPPID ]; then
echo "Shutdown $1 process:$APPPID"
kill -9 $APPPID
else
echo "Process $1 NOT found."
fi
解释:
ps aux 表示查询进程列表
grep $1 表示匹配参数1
grep -v “grep” 表示排除包含grep的行
grep -v “$0” 表示排除当前执行脚本的行
head -n 1 表示只取第一行
cut -c 9-15表示截取第9-15个字符
root 1052 0.0 0.0 105996 4120 ? Ss 2020 0:00 /usr/sbin/sshd -D
root 19695 0.0 0.0 148316 5496 ? Ss 1月19 0:00 sshd: root@notty
root 25969 0.0 0.0 148316 5400 ? Ss 12:09 0:00 sshd: root@pts/0
root 30172 0.0 0.0 112676 984 pts/0 R+ 13:35 0:00 grep --color=auto ssh