搜索"xxxx"的进程,同时杀进程

一、搜索"xxxx"的进程,同时杀进程,命令如下:

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

命令解释:

  1. ps:

    -a 显示所有进程 ;

    -e 等价于-A,显示所有进程;

    -f 全部列出,通常和其他参数一起使用;

    ps -eaf意思:显示所有进程;

  2. grep 是查找含有指定文本行的意思,比如grep test 就是查找含有test的文本的行

  3. grep -v 是反向查找的意思,那么grep -v grep意思即:过滤掉包含有grep字符的行
          -i 不区分大小写,那么grep -i nodemanager意思即:查找包含nodemanager的行;

  4. awk '{print $n}' 中的$0则表示所有域,$1表示第一个域,$n表示第n个域。默认域分隔符是"空白键" 或 "[tab]键"。

   awk '{print $2}'意思:打印查找出来的内容的第二个域

  5. xargs 命令把前面的输出作为后面命令kill -9 的输入,也就是传左边命令截取的进程号给命令kill -9

 二、用nignx进程举例:

[root@ZWZF-CWY-LZY-12 sbin]# ps -ef|grep nginx
root      9801     1  0 15:30 ?        00:00:00 nginx: master process ./nginx
root      9802  9801  0 15:30 ?        00:00:00 nginx: worker process
root      9803  9801  0 15:30 ?        00:00:00 nginx: worker process
root      9804  9801  0 15:30 ?        00:00:00 nginx: worker process
root      9805  9801  0 15:30 ?        00:00:00 nginx: worker process
root      9806  9801  0 15:30 ?        00:00:00 nginx: worker process
root      9807  9801  0 15:30 ?        00:00:00 nginx: worker process
root      9808  9801  0 15:30 ?        00:00:00 nginx: worker process
root      9809  9801  0 15:30 ?        00:00:00 nginx: worker process
root      9814  9365  0 15:30 pts/1    00:00:00 grep --color=auto nginx
[root@ZWZF-CWY-LZY-12 sbin]# ps -ef|grep nginx|grep -v 'grep'|awk '{print $2}'
9801
9802
9803
9804
9805
9806
9807
9808
9809
[root@ZWZF-CWY-LZY-12 sbin]# ps -ef|grep nginx|grep -v 'grep'|awk '{print $2}'|xargs kill -9
[root@ZWZF-CWY-LZY-12 sbin]# ps -ef|grep nginx
root      9863  9365  0 15:30 pts/1    00:00:00 grep --color=auto nginx
[root@ZWZF-CWY-LZY-12 sbin]#

 

posted @ 2022-09-28 16:38  查拉图斯特拉面条  阅读(48)  评论(0编辑  收藏  举报