zoukankan      html  css  js  c++  java
  • linux下杀死进程(kill)的N种方法

    首先,用ps查看进程,方法如下:

    ps -ef

    UID        PID  PPID  C STIME TTY          TIME CMD
    root         1     0  0 Jun15 ?        00:00:01 /sbin/init
    root         2     0  0 Jun15 ?        00:00:00 [kthreadd]
    root         3     2  0 Jun15 ?        00:00:00 [migration/0]
    root         4     2  0 Jun15 ?        00:00:02 [ksoftirqd/0]
    root         5     2  0 Jun15 ?        00:00:00 [stopper/0]
    root         6     2  0 Jun15 ?        00:00:04 [watchdog/0]
    root         7     2  0 Jun15 ?        00:00:00 [migration/1]
    root         8     2  0 Jun15 ?        00:00:00 [stopper/1]
    root         9     2  0 Jun15 ?        00:00:00 [ksoftirqd/1]
    root        10     2  0 Jun15 ?        00:00:02 [watchdog/1]
    ……
    

    或者:

    ps -aux

    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root         1  0.0  0.0  19232  1504 ?        Ss   Jun15   0:01 /sbin/init
    root         2  0.0  0.0      0     0 ?        S    Jun15   0:00 [kthreadd]
    root         3  0.0  0.0      0     0 ?        S    Jun15   0:00 [migration/0]
    root         4  0.0  0.0      0     0 ?        S    Jun15   0:02 [ksoftirqd/0]
    root         5  0.0  0.0      0     0 ?        S    Jun15   0:00 [stopper/0]
    root         6  0.0  0.0      0     0 ?        S    Jun15   0:04 [watchdog/0]
    root         7  0.0  0.0      0     0 ?        S    Jun15   0:00 [migration/1]
    root         8  0.0  0.0      0     0 ?        S    Jun15   0:00 [stopper/1]
    root         9  0.0  0.0      0     0 ?        S    Jun15   0:00 [ksoftirqd/1]
    root        10  0.0  0.0      0     0 ?        S    Jun15   0:02 [watchdog/1]
    ……
    

    此时如果我想杀了mysql的进程就在终端输入:

    kill -s 9 1827

    其中-s 9 制定了传递给进程的信号是9,即强制、尽快终止进程。各个终止信号及其作用见附录。

    1827则是上面ps查到的mysql的PID。

    简单吧,但有个问题,进程少了则无所谓,进程多了,就会觉得痛苦了,无论是ps -ef 还是ps -aux,每次都要在一大串进程信息里面查找到要杀的进程,看的眼都花了。

    进阶篇:

    改进1:

    把ps的查询结果通过管道给grep查找包含特定字符串的进程。管道符“|”用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令的输入。

    ps -ef | grep mysql

    root      6849 31342  0 18:19 pts/0    00:00:00 grep mysql
    root     12373     1  0 Oct12 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
    mysql    12591 12373  2 Oct12 ?        14:44:30 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
    

    然后就是
    kill -s 9 12591

    改进2——使用pgrep:

    一看到pgrep首先会想到什么?没错,grep!pgrep的p表明了这个命令是专门用于进程查询的grep。

    pgrep mysql

    12591 
    

    然后就是
    kill -s 9 12591

    改进3——使用pidof:

    看到pidof想到啥?没错pid of xx,字面翻译过来就是 xx的PID。

    pidof mysql

    12591 
    

    和pgrep相比稍显不足的是,pidof必须给出进程的全名。然后就是老生常谈:

    kill -s 9 12591

    无论使用ps 然后慢慢查找进程PID 还是用grep查找包含相应字符串的进程,亦或者用pgrep直接查找包含相应字符串的进程pid,然后手动输入给kill杀掉,都稍显麻烦。有没有更方便的方法?有!

    改进4:

    ps -ef | grep mysql | grep -v grep | cut -c 9-15 | xargs kill -s 9

    说明:

    “grep mysql”的输出结果是,所有含有关键字“mysql”的进程。

    “grep -v grep”是在列出的进程中去除含有关键字“grep”的进程。

    “cut -c 9-15”是截取输入行的第9个字符到第15个字符,而这正好是进程号PID。

    “xargs kill -s 9”中的xargs命令是用来把前面命令的输出结果(PID)作为“kill -s 9”命令的参数,并执行该命令。“kill -s 9”会强行杀掉指定进程。

    难道你不想抱怨点什么?没错太长了

    改进5:

    知道pgrep和pidof两个命令,干嘛还要打那么长一串!

    pgrep mysql | xargs kill -s 9

    改进6:

    ps -ef | grep mysql | awk '{print $2}' | xargs kill -9

    kill: No such process
    

    有一个比较郁闷的地方,进程已经正确找到并且终止了,但是执行完却提示找不到进程。

    其中awk '{print $2}' 的作用就是打印(print)出第二列的内容。根据常规篇,可以知道ps输出的第二列正好是PID。就把进程相应的PID通过xargs传递给kill作参数,杀掉对应的进程。

    改进7:

    难道每次都要调用xargs把PID传递给kill?答案是否定的:

    kill -s 9 `ps -aux | grep mysql | awk '{print $2}'`
    

    改进8:

    没错,命令依然有点长,换成pgrep。

    kill -s 9 `pgrep mysql`
    

    改进9——pkill:

    看到pkill想到了什么?没错pgrep和kill!pkill=pgrep+kill。

    pkill -9 mysql
    

    说明:"-9" 即发送的信号是9,pkill与kill在这点的差别是:pkill无须 “s”,终止信号等级直接跟在 “-“ 后面。之前我一直以为是 "-s 9",结果每次运行都无法终止进程。

    改进10——killall:

    killall和pkill是相似的,不过如果给出的进程名不完整,killall会报错。pkill或者pgrep只要给出进程名的一部分就可以终止进程。

    killall -9 mysql
    

  • 相关阅读:
    微软外服 AlI In One
    js 循环多次和循环一次的时间的性能对比 All In One
    vue inject All In One
    Excel 表格数据倒置 All In One
    SVG tickets All In One
    OH MY ZSH All In One
    js array for loop performance compare All In One
    mac terminal show You have new mail All In one
    新闻视频 26 制作母版页
    转自牛腩 母版页和相对路径
  • 原文地址:https://www.cnblogs.com/niuben/p/13927371.html
Copyright © 2011-2022 走看看