• 请不要在回答技术问题时复制粘贴 AI 生成的内容
bramblex
V2EX  ›  程序员

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

  •  
  •   bramblex ·
    bramblex · Jul 29, 2015 · 4287 views
    This topic created in 4026 days ago, the information mentioned may be changed or developed.

    使用方式: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
    
    Supplement 1  ·  Jul 29, 2015
    和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 replies    2015-10-16 18:23:08 +08:00
    imn1
        1
    imn1  
       Jul 29, 2015
    能判别是否使用 sudo 更好
    582033
        2
    582033  
       Jul 29, 2015 via Android
    恭喜楼主又造了个轮子……
    lingo233
        3
    lingo233  
       Jul 29, 2015 via iPhone
    不是有killall码?
    582033
        4
    582033  
       Jul 29, 2015 via Android
    pkill
    bramblex
        5
    bramblex  
    OP
       Jul 29, 2015
    @imn1 如果提示not permitted的话直接 `sudo !!` 就好了……这是基本啊。
    ETiV
        6
    ETiV  
       Jul 29, 2015
    pgrep / pkill
    bramblex
        7
    bramblex  
    OP
       Jul 29, 2015
    @582033 这还是小轮子 /w\
    bramblex
        8
    bramblex  
    OP
       Jul 29, 2015
    @lingo233 能模糊匹配吗?我就是懒而已……
    bramblex
        9
    bramblex  
    OP
       Jul 29, 2015
    @ETiV

    然而……我懒
    TerrenceSun
        10
    TerrenceSun  
       Jul 29, 2015
    killall
    bramblex
        11
    bramblex  
    OP
       Jul 29, 2015
    @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
       Jul 29, 2015
    @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  
       Jul 29, 2015
    再填一个根据端口号kill
    bramblex
        14
    bramblex  
    OP
       Jul 29, 2015
    @ChanneW

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

    两句命令搞定

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

    pgrep PROCESS_NAME

    kill PID
    bramblex
        25
    bramblex  
    OP
       Jul 31, 2015
    随手看了楼上的其他回复,已block。
    acgeo
        26
    acgeo  
       Jul 31, 2015
    @bramblex zhuangbility
    TerrenceSun
        27
    TerrenceSun  
       Aug 3, 2015
    @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  
       Oct 16, 2015
    要是进程名是数字怎么办 OwO
    bramblex
        29
    bramblex  
    OP
       Oct 16, 2015
    @breeswish 那就无解了 /w\
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4213 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 72ms · UTC 05:33 · PVG 13:33 · LAX 22:33 · JFK 01:33
    ♥ Do have faith in what you're doing.