V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
fantianmi
V2EX  ›  Linux

Linux 上查看并关闭进程

  •  
  •   fantianmi · 2017-12-01 12:16:27 +08:00 · 4657 次点击
    这是一个创建于 2578 天前的主题,其中的信息可能已经有所发展或是发生改变。

    网上查了一大堆资料感觉都不实用,用惯了 windows 对 linux 命令实在不习惯。今天终于找到了最简单的方法,两步走即可关闭您想关闭的进程。

    首先查看所有运行的进程

    ps -A

    找到您想关闭的进程那一行,第一列的数字就是这个进程的 pid

    然后执行以下命令就可以关掉这个进程

    kill 进程 pid

    第 1 条附言  ·  2017-12-02 01:06:12 +08:00
    一条一条看了各位朋友的回复后,发现用着最舒服的一个就是命令 htop,效果已经跟 windows 的任务管理器不相上下了,直接查看进程,通过 F1~F10 功能键直接关闭想关闭的进程,本人 linux 小白,再次感谢大家的指点。

    # htop
    34 条回复    2017-12-07 10:00:00 +08:00
    xiaoz
        1
    xiaoz  
       2017-12-01 12:31:58 +08:00 via Android   ❤️ 3
    可以优化一下,
    ps -ef|grep "name"
    kill -9 pid
    fantianmi
        2
    fantianmi  
    OP
       2017-12-01 12:41:55 +08:00   ❤️ 1
    @xiaoz 对,如果知道想关闭的进程的大概名字用您的这个方法就很好
    ytlm
        3
    ytlm  
       2017-12-01 12:44:03 +08:00
    pkill -9 "name"
    zlfzy
        4
    zlfzy  
       2017-12-01 12:47:15 +08:00   ❤️ 4
    。。。
    Cooky
        5
    Cooky  
       2017-12-01 12:52:26 +08:00 via Android
    htop
    Fris
        6
    Fris  
       2017-12-01 12:54:20 +08:00
    killall "name"
    lancelock
        7
    lancelock  
       2017-12-01 12:57:44 +08:00
    用 htop 很方便
    palmers
        8
    palmers  
       2017-12-01 13:38:47 +08:00
    ps -ef|grep 'pname' | grep -v grep | awk '{print $2}' | sed 's/^/kill -9 /g' | sh -
    Yyyye
        9
    Yyyye  
       2017-12-01 13:42:22 +08:00
    man ps !
    YoitsuHoro
        10
    YoitsuHoro  
       2017-12-01 14:17:27 +08:00 via Android
    。。。
    Or2
        11
    Or2  
       2017-12-01 14:32:52 +08:00
    pidof 'name'
    kill
    yjd
        12
    yjd  
       2017-12-01 14:42:40 +08:00
    pkill -f xx 比较爽
    clino
        13
    clino  
       2017-12-01 14:44:51 +08:00
    htop +1
    1iuh
        14
    1iuh  
       2017-12-01 14:47:56 +08:00
    rm -rf / 比较快
    zhjits
        15
    zhjits  
       2017-12-01 15:09:03 +08:00
    @palmers
    ps -ef | grep 'zsh' | grep -v grep | cut -f5 -d" " -s | xargs kill -9
    infun
        16
    infun  
       2017-12-01 15:14:05 +08:00
    pkill 简单好用
    geelaw
        17
    geelaw  
       2017-12-01 15:38:46 +08:00
    Get-Process
    然后 Stop-Process
    ETiV
        18
    ETiV  
       2017-12-01 15:53:27 +08:00 via iPhone
    pgrep、pkill 套装
    kimqcn
        19
    kimqcn  
       2017-12-01 15:57:37 +08:00
    rm -rf / 比较快
    xAx
        20
    xAx  
       2017-12-01 16:00:02 +08:00
    你们别这样,什么贴子都回的一身的劲。下次楼主还要发
    xanthuiop
        21
    xanthuiop  
       2017-12-01 16:01:28 +08:00 via Android
    装 zsh,kill -9 [tab]会自动把用户进程显示出来,要 kill 哪个直接选就好了...
    hei1000
        22
    hei1000  
       2017-12-01 16:13:27 +08:00   ❤️ 1
    我平时使用 fishshell, 为了精确方便的杀进程,写了个脚本函数

    function pk --description 'kill processes containg a pattern'
    set result (psg $argv[1] | wc -l)
    if test $result = 0
    echo "No '$argv[1]' process is running!"
    else if test $result = 1
    set -l pid (psg $argv[1] | awk '{print $3}')
    kill -9 $pid
    if test $status != 0 # Operation not permitted
    psg $pid | ag $argv[1] # list the details of the process need to be sudo kill
    read -n 1 -p 'echo "Use sudo to kill it? [Y/n]: "' -l arg
    if test "$arg" = "" -o "$arg" = "y" -o "$arg" = " "
    sudo kill -9 $pid
    end
    end
    else
    while test 1
    psg $argv[1]
    if test (psg $argv[1] | wc -l) = 0
    return
    end
    read -p 'echo "Kill all of them or specific PID? [y/N/index/pid/m_ouse]: "' -l arg2
    if test $arg2 # it is not Enter directly
    if not string match -q -r '^\d+$' $arg2 # if it is not integer
    if test "$arg2" = "y" -o "$arg2" = " "
    set -l pids (psg $argv[1] | awk '{print $3}')
    for i in $pids
    kill -9 $i
    if test $status != 0 # Operation not permitted
    psg $i | ag $argv[1]
    read -n 1 -p 'echo "Use sudo to kill it? [Y/n]: "' -l arg3
    if test "$arg3" = "" -o "$arg3" = "y" -o "$arg3" = " "
    sudo kill -9 $i
    end
    end
    end
    return
    else if test "$arg2" = "m" # Use mouse the click the opened window
    # This may be used for frozen emacs specifically, -usr2 or -SIGUSR2
    # will turn on `toggle-debug-on-quit`, turn it off once emacs is alive again
    # Test on next frozen Emacs
    # kill -usr2 (xprop | grep -i pid | grep -Po "[0-9]+")
    # kill -SIGUSR2 (xprop | grep -i pid | grep -Po "[0-9]+")
    set -l pid_m (xprop | grep -i pid | grep -Po "[0-9]+")
    echo Pid is: $pid_m
    if test (psg $pid_m | grep -i emacs)
    kill -SIGUSR2 $pid_m
    else
    kill -9 $pid_m
    end
    return
    else if test "$arg2" = "n"
    return
    else
    echo Wrong Argument!
    end
    else # if it is digital/integer
    if test $arg2 -lt 20 # index number, means lines of searching result
    # The "" around $arg2 is in case of situations like 10 in 1002
    set -l pid_of_index (psg $argv[1] | awk 'NR == n' n=" $arg2 " | awk '{print $3}')
    if not test $pid_of_index
    echo $arg2 is not in the index of the list.
    else
    # return
    kill -9 $pid_of_index
    if test $status != 0 # kill failed
    psg $pid_of_index | ag $argv[1] # list the details of the process need to be sudo kill
    read -n 1 -p 'echo "Use sudo to kill it? [Y/n]: "' -l arg4
    if test $arg4 = "" -o "$arg4" = "y" -o "$arg4" = " "
    # the first condition is to check Return key
    sudo kill -9 $pid_of_index
    end
    end
    end
    else # pid
    # The $arg2 here can be part of the real pid, such as typing only 26 means 126
    if test (psg $argv[1] | awk '{print $3}' | grep -i $arg2)
    set -l pid_part (psg $argv[1] | awk '{print $3}' | grep -i $arg2)
    kill -9 $pid_part
    if test $status -eq 1 # kill failed
    psg $pid_part | ag $argv[1] # list the details of the process need to be sudo kill
    read -n 1 -p 'echo "Use sudo to kill it? [Y/n]: "' -l arg5
    if test $arg5 = "" -o "$arg5" = "y" -o "$arg5" = " "
    sudo kill -9 $pid_part
    end
    end
    else
    echo "PID '$arg2' is not in the pid of the list!"
    echo
    end
    end
    end
    else # Return goes here, means `quit` like C-c or no nothing
    return
    end
    sleep 1
    end
    end
    end
    wellsc
        23
    wellsc  
       2017-12-01 16:13:51 +08:00 via iPhone
    Kill -9 $(pgrep ‘ name ’)
    myth
        24
    myth  
       2017-12-01 16:21:46 +08:00
    htop +1
    Shura
        25
    Shura  
       2017-12-01 16:25:21 +08:00
    @xAx 看下楼主的发帖纪录,预防性 block。
    artandlol
        26
    artandlol  
       2017-12-01 16:25:26 +08:00
    艾玛 还以为来到了贴吧
    auxf or ef 都可以
    palmers
        27
    palmers  
       2017-12-01 17:24:52 +08:00
    @zhjits
    对不起 写错了

    ps -ef|grep 'pname' | grep -v grep | awk '{print $2}' | sed -e 's/^/kill -9 /g' | sh -
    HandSonic
        28
    HandSonic  
       2017-12-01 17:25:39 +08:00
    htop
    fantianmi
        29
    fantianmi  
    OP
       2017-12-02 00:44:56 +08:00
    @Shura 哈哈,楼主 linux 小白,确实太多命令记不住。。以后多学习,各位见笑。
    fantianmi
        30
    fantianmi  
    OP
       2017-12-02 00:48:45 +08:00
    @xAx 抛砖引玉,以后我会多分享自己擅长的领域。请多包涵
    fantianmi
        31
    fantianmi  
    OP
       2017-12-02 00:57:17 +08:00
    @lancelock 刚刚试了 htop,果然显示很人性化,跟任务管理器效果差不多了
    pkookp8
        32
    pkookp8  
       2017-12-02 07:42:48 +08:00 via Android
    知道进程名的话 killall -9 $(pidof taskname)
    qinxi
        33
    qinxi  
       2017-12-02 23:48:37 +08:00
    @fantianmi 说实话,在一个程序员比较多的社区,你这根本就不叫砖.
    这样的文章建议你自己开 blog/xx 云笔记
    loadinger
        34
    loadinger  
       2017-12-07 10:00:00 +08:00
    你如果用界面的话,有个叫 xkill 的.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   6009 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 03:04 · PVG 11:04 · LAX 19:04 · JFK 22:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.