[居然没有 shellscript 节点]随手写了一个小脚本,帮助你 kill 进程。

2015-07-29 18:13:02 +08:00
 bramblex

使用方式:mykill <进程名或pid>

>> 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
3548 次点击
所在节点    程序员
29 条回复
imn1
2015-07-29 18:21:46 +08:00
能判别是否使用 sudo 更好
582033
2015-07-29 18:21:58 +08:00
恭喜楼主又造了个轮子……
lingo233
2015-07-29 18:22:31 +08:00
不是有killall码?
582033
2015-07-29 18:22:42 +08:00
pkill
bramblex
2015-07-29 18:40:47 +08:00
@imn1 如果提示not permitted的话直接 `sudo !!` 就好了……这是基本啊。
ETiV
2015-07-29 18:41:38 +08:00
pgrep / pkill
bramblex
2015-07-29 18:41:42 +08:00
@582033 这还是小轮子 /w\
bramblex
2015-07-29 18:43:24 +08:00
@lingo233 能模糊匹配吗?我就是懒而已……
bramblex
2015-07-29 18:44:37 +08:00
@ETiV

然而……我懒
TerrenceSun
2015-07-29 19:08:27 +08:00
killall
bramblex
2015-07-29 19:18:31 +08:00
@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]
```
bramblex
2015-07-29 19:23:00 +08:00
@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这种傻逼玩意的?
ChanneW
2015-07-29 19:23:22 +08:00
再填一个根据端口号kill
bramblex
2015-07-29 19:28:56 +08:00
@ChanneW

好主意!这个怎么能忘记!!!
omph
2015-07-29 20:58:59 +08:00
源里面有没有类似的?
cnallenzhao
2015-07-29 22:52:51 +08:00
QJ你需要个Alfred,哈哈
winkidney
2015-07-29 23:59:20 +08:00
默默路过
mongodb
2015-07-30 00:00:51 +08:00
我关心的是居然真的没有Shell这个节点的问题。。
loggerhead
2015-07-30 07:01:49 +08:00
用oh-my-zsh,kill还能自动补全,爽得不行
bramblex
2015-07-30 09:15:55 +08:00
@loggerhead 一样的,补全要求至少知道开头几个字母,但是我这货只要求你知道随便几个连续字母,高下立判

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/209343

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX