想一次性删除多个pcap_dir进程
ps -a | grep pcap_dir | cut -c 1-5 | xargs kill -9
原理如下:
[root@localhost replay-packets]# ps -a
PID TTY TIME CMD
2625 pts/0 00:00:00 pcap_dir
3507 pts/0 00:00:00 tcpreplay
13913 pts/0 00:00:00 pcap_dir
13915 pts/0 00:47:04 tcpreplay
14609 pts/0 00:00:00 pcap_dir
14611 pts/0 00:00:39 tcpreplay
14617 pts/0 00:00:00 pcap_dir
17004 pts/0 00:07:31 tcpreplay
17184 pts/0 00:00:00 ps
22851 pts/0 00:00:00 su
22852 pts/0 00:00:05 bash
[root@localhost replay-packets]# ps -a | grep pcap_dir
2625 pts/0 00:00:00 pcap_dir
13913 pts/0 00:00:00 pcap_dir
14609 pts/0 00:00:00 pcap_dir
14617 pts/0 00:00:00 pcap_dir
[root@localhost replay-packets]# ps -a | grep pcap_dir | cut -c 1-5
2625
13913
14609
14617
[root@localhost replay-packets]# ps -a | grep pcap_dir | cut -c 1-5 | xargs
2625 13913 14609 14617
[root@localhost replay-packets]# ps -a | grep pcap_dir | cut -c 1-5 | xargs kill -9
[1] Killed ./pcap_dir ips_normal/ eth3004
[2] Killed ./pcap_dir av/ eth3005 500
[3]- Killed ./pcap_dir av/ eth3005 1000
[4]+ Killed ./pcap_dir av/ eth3005 1000
[root@localhost replay-packets]#
[root@localhost replay-packets]#
从最后一个命令看,xargs的作用是获取参数,并逐一作为后续命令的参数输入