使用方式:mykill <进程名或pid>
1234
,则会被当成是pid。mykill会直接询问你是否要kill到这个进程。qq
,则mykill会自动匹配包含这个字符串的进程。如果包含这个字符串的进程只有一个,那么mykill会询问是否kill掉这个进程。如果包含这个字符串的进程有多个,则会打印一个进程列表。>> mykill 10150 #参数为pid
kill 10150 /Applications/QQ.app/Contents/MacOS/QQ? [y/n]
>> mykill qq #参数为进程名,只match到一个结果
kill 10150 /Applications/QQ.app/Contents/MacOS/QQ? [y/n]
>> mykill sh #参数为进程名,match到多个结果
22340 brambles /bin/bash
10180 brambles -zsh
22341 brambles /bin/bash
43369 brambles -zsh
43262 brambles /bin/bash
7747 brambles -zsh
matched 6 processes!
>> mykill abc #没有match到结果
no thing matched!
=====================以下是脚本代码====================
将一下代码保存到一个文件里面,并给文件执行权限就行了。
#!/bin/bash
if [[ $1 =~ ^[0-9]+$ ]]
then
p_l=$(ps aux | awk -v pid="$1" 'NR > 1 && $2 == pid {print $2, $1, $11}')
else
p_l=$(ps aux | awk -v pname="$1" 'NR > 1 && tolower($11) ~ tolower(pname) {print $2, $1, $11}')
fi
l_n=$(echo "${p_l}" | grep -v ^\s*$ |wc -l)
l_n=$(echo ${l_n})
if [ ${l_n} == 0 ]
then
echo "no thing matched!"
elif [ ${l_n} == 1 ]
then
p_pid=$(echo "${p_l}"| awk '{print $1}')
p_name=$(echo "${p_l}"| awk '{print $3}')
read -p "kill ${p_pid} ${p_name}? [y/n]" -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
kill -9 ${p_pid}
fi
echo
else
echo "${p_l}"
echo "matched ${l_n} processes!"
fi
1
imn1 2015-07-29 18:21:46 +08:00
能判别是否使用 sudo 更好
|
2
582033 2015-07-29 18:21:58 +08:00 via Android
恭喜楼主又造了个轮子……
|
3
lingo233 2015-07-29 18:22:31 +08:00 via iPhone
不是有killall码?
|
4
582033 2015-07-29 18:22:42 +08:00 via Android
pkill
|
6
ETiV 2015-07-29 18:41:38 +08:00
pgrep / pkill
|
10
TerrenceSun 2015-07-29 19:08:27 +08:00
killall
|
11
bramblex OP @TerrenceSun 给你看对比
``` >> killall qq No matching processes belonging to you were found >> mykill qq kill 10150 /Applications/QQ.app/Contents/MacOS/QQ? [y/n] >> killall we No matching processes belonging to you were found >> mykill we 23219 brambles /System/Library/Frameworks/WebKit.framework/Versions/A/XPCServices/com.apple.WebKit.WebContent.xpc/Contents/MacOS/com.apple.WebKit.WebContent 70147 brambles /System/Library/PrivateFrameworks/WeatherKit.framework/Versions/A/XPCServices/com.apple.WeatherKitService.xpc/Contents/MacOS/com.apple.WeatherKitService 444 brambles /System/Library/CoreServices/NotificationCenter.app/Contents/XPCServices/com.apple.notificationcenterui.WeatherSummary.xpc/Contents/MacOS/com.apple.notificationcenterui.WeatherSummary 51 root /System/Library/CoreServices/powerd.bundle/powerd 23230 brambles /Applications/WeChat.app/Contents/MacOS/WeChat matched 5 processes! >> killall wechat No matching processes belonging to you were found >>mykill wechat kill 23230 /Applications/WeChat.app/Contents/MacOS/WeChat? [y/n] ``` |
12
bramblex OP @TerrenceSun
>> killall wec No matching processes belonging to you were found >> mykill wec kill 23230 /Applications/WeChat.app/Contents/MacOS/WeChat? [y/n] >> killall 23414 No matching processes belonging to you were found >> mykill 23414 kill 23414 /Applications/QQ.app/Contents/MacOS/QQ? [y/n]y 对比看出来了吗?或说你们是怎么忍受killall这种傻逼玩意的? |
13
ChanneW 2015-07-29 19:23:22 +08:00
再填一个根据端口号kill
|
15
omph 2015-07-29 20:58:59 +08:00
源里面有没有类似的?
|
16
cnallenzhao 2015-07-29 22:52:51 +08:00
QJ你需要个Alfred,哈哈
|
17
winkidney 2015-07-29 23:59:20 +08:00
默默路过
|
18
mongodb 2015-07-30 00:00:51 +08:00
我关心的是居然真的没有Shell这个节点的问题。。
|
19
loggerhead 2015-07-30 07:01:49 +08:00 via iPhone
用oh-my-zsh,kill还能自动补全,爽得不行
|
20
bramblex OP @loggerhead 一样的,补全要求至少知道开头几个字母,但是我这货只要求你知道随便几个连续字母,高下立判
|
23
bramblex OP @cnallenzhao 没折腾过,下次试试。
|
24
acgeo 2015-07-30 11:27:56 +08:00
整这么复杂 有毛用
两句命令搞定 PROCESS_NAME为想kill的进程 pgrep后会输出PID号 然后kill 你也可以把这两句写成一句~~~ pgrep PROCESS_NAME kill PID |
25
bramblex OP 随手看了楼上的其他回复,已block。
|
27
TerrenceSun 2015-08-03 19:37:58 +08:00
@bramblex killall没有这么不济吧,-r可以用正则式来匹配。要是要求按参数而不是命令本身进行匹配的话,那的确不如自己造的顺手。
Usage: killall [-Z CONTEXT] [-u USER] [ -eIgiqrvw ] [ -SIGNAL ] NAME... killall -l, --list killall -V, --version -e,--exact require exact match for very long names -I,--ignore-case case insensitive process name match -g,--process-group kill process group instead of process -i,--interactive ask for confirmation before killing -l,--list list all known signal names -q,--quiet don't print complaints -r,--regexp interpret NAME as an extended regular expression -s,--signal SIGNAL send this signal instead of SIGTERM -u,--user USER kill only process(es) running as USER -v,--verbose report if the signal was successfully sent -V,--version display version information -w,--wait wait for processes to die -Z,--context REGEXP kill only process(es) having context (must precede other arguments) |
28
breeswish 2015-10-16 18:17:48 +08:00
要是进程名是数字怎么办 OwO
|