简体   繁体   English

如何杀死描述中包含'/var/'字符串的所有进程?

[英]How to kill all processes which have '/var/' string in the description?

I am using lsof to find out all processes which have /var/ in the description.我正在使用lsof找出描述中包含/var/的所有进程。

[root@localhost ~]# lsof | grep /var/ | less
systemd      1                          root  105u     unix 0xffff9b25fae49680      0t0      21311 /var/run/cups/cups.sock type=STREAM
systemd      1                          root  134u     unix 0xffff9b25f509cd80      0t0      21334 /var/run/libvirt/virtlogd-sock type=STREAM
systemd      1                          root  135u     unix 0xffff9b25fae49f80      0t0      21315 /var/run/.heim_org.h5l.kcm-socket type=STREAM
systemd      1                          root  137u     unix 0xffff9b25fae48d80      0t0      21318 /var/run/libvirt/virtlockd-sock type=STREAM
polkitd    745                       polkitd  mem       REG                8,3 10406312    2107847 /var/lib/sss/mc/initgroups
polkitd    745                       polkitd  mem       REG                8,3  6406312    2107846 /var/lib/sss/mc/group

I am now trying to kill all these processes using the following command:我现在正在尝试使用以下命令杀死所有这些进程:

kill -9 $(lsof | grep /var/)

But getting error:但出现错误:

-bash: kill: root: arguments must be process or job IDs
-bash: kill: 8w: arguments must be process or job IDs
-bash: kill: REG: arguments must be process or job IDs
-bash: kill: 8,3: arguments must be process or job IDs

You want to你想要

  • match on just the NAME field, and仅匹配 NAME 字段,并且
  • print out just the pid, not the whole line:只打印 pid,而不是整行:
lsof | awk -v pattern="/var/" '$9 ~ pattern {print $2}'

On my system, that seems to output lots of duplicates:在我的系统上,output 似乎有很多重复项:

lsof | awk -v pattern="/var/" '$9 ~ pattern {print $2}' | sort -u

and pass that list of pids to kill using xargs并使用 xargs 传递要杀死的 pid 列表

lsof | awk -v pattern="/var/" '$9 ~ pattern {print $2}' | sort -u | xargs echo kill

Remove "echo" if you're satisfied it's working as you expect.如果您对它按预期工作感到满意,请删除“回声”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM