V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
bramblex
V2EX  ›  程序员

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

  •  
  •   bramblex ·
    bramblex · 2015-07-29 18:13:02 +08:00 · 3630 次点击
    这是一个创建于 3407 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用方式: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 条附言  ·  2015-07-29 19:26:46 +08:00
    和killall的对比。大家到底怎么忍受killall这种傻逼玩意的?

    >> 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]

    >> 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
    29 条回复    2015-10-16 18:23:08 +08:00
    imn1
        1
    imn1  
       2015-07-29 18:21:46 +08:00
    能判别是否使用 sudo 更好
    582033
        2
    582033  
       2015-07-29 18:21:58 +08:00 via Android
    恭喜楼主又造了个轮子……
    lingo233
        3
    lingo233  
       2015-07-29 18:22:31 +08:00 via iPhone
    不是有killall码?
    582033
        4
    582033  
       2015-07-29 18:22:42 +08:00 via Android
    pkill
    bramblex
        5
    bramblex  
    OP
       2015-07-29 18:40:47 +08:00
    @imn1 如果提示not permitted的话直接 `sudo !!` 就好了……这是基本啊。
    ETiV
        6
    ETiV  
       2015-07-29 18:41:38 +08:00
    pgrep / pkill
    bramblex
        7
    bramblex  
    OP
       2015-07-29 18:41:42 +08:00
    @582033 这还是小轮子 /w\
    bramblex
        8
    bramblex  
    OP
       2015-07-29 18:43:24 +08:00
    @lingo233 能模糊匹配吗?我就是懒而已……
    bramblex
        9
    bramblex  
    OP
       2015-07-29 18:44:37 +08:00
    @ETiV

    然而……我懒
    TerrenceSun
        10
    TerrenceSun  
       2015-07-29 19:08:27 +08:00
    killall
    bramblex
        11
    bramblex  
    OP
       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
        12
    bramblex  
    OP
       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
        13
    ChanneW  
       2015-07-29 19:23:22 +08:00
    再填一个根据端口号kill
    bramblex
        14
    bramblex  
    OP
       2015-07-29 19:28:56 +08:00
    @ChanneW

    好主意!这个怎么能忘记!!!
    omph
        15
    omph  
       2015-07-29 20:58:59 +08:00
    源里面有没有类似的?
    cnallenzhao
        16
    cnallenzhao  
       2015-07-29 22:52:51 +08:00
    QJ你需要个Alfred,哈哈
    winkidney
        17
    winkidney  
       2015-07-29 23:59:20 +08:00
    默默路过
    mongodb
        18
    mongodb  
       2015-07-30 00:00:51 +08:00
    我关心的是居然真的没有Shell这个节点的问题。。
    loggerhead
        19
    loggerhead  
       2015-07-30 07:01:49 +08:00 via iPhone
    用oh-my-zsh,kill还能自动补全,爽得不行
    bramblex
        20
    bramblex  
    OP
       2015-07-30 09:15:55 +08:00 via Smartisan T1
    @loggerhead 一样的,补全要求至少知道开头几个字母,但是我这货只要求你知道随便几个连续字母,高下立判
    bramblex
        21
    bramblex  
    OP
       2015-07-30 09:16:19 +08:00 via Smartisan T1
    @mongodb 对啊,不知道为什么没有
    bramblex
        22
    bramblex  
    OP
       2015-07-30 09:20:06 +08:00 via Smartisan T1
    @winkidney 活捉一只基佬
    bramblex
        23
    bramblex  
    OP
       2015-07-30 09:20:50 +08:00 via Smartisan T1
    @cnallenzhao 没折腾过,下次试试。
    acgeo
        24
    acgeo  
       2015-07-30 11:27:56 +08:00
    整这么复杂 有毛用

    两句命令搞定

    PROCESS_NAME为想kill的进程 pgrep后会输出PID号 然后kill 你也可以把这两句写成一句~~~

    pgrep PROCESS_NAME

    kill PID
    bramblex
        25
    bramblex  
    OP
       2015-07-31 02:45:40 +08:00
    随手看了楼上的其他回复,已block。
    acgeo
        26
    acgeo  
       2015-07-31 10:16:41 +08:00
    @bramblex zhuangbility
    TerrenceSun
        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)
    breeswish
        28
    breeswish  
       2015-10-16 18:17:48 +08:00
    要是进程名是数字怎么办 OwO
    bramblex
        29
    bramblex  
    OP
       2015-10-16 18:23:08 +08:00
    @breeswish 那就无解了 /w\
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3245 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:57 · PVG 20:57 · LAX 04:57 · JFK 07:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.