有时候我们会遇到需要将指定命令返回结果进行处理的情况
这种情况下,可能就需要写for循环之类的脚本进行处理了(目前我只能想到这种方法)
但是想起来还有一个xargs命令,组合这个命令就比较省事了。
场景如下:
安装Redis执行make test时,报Redis已在运行导致冲突。ps -ef查看Redis,发现还真的有十来个redis进程再跑。那就只能把其全部关闭了。一个一个kill不现实,于是研究了一下xargs命令来实现。以下是我的做法
1.首先我们需要获取到所有redis进程信息
ps -ef|grep redis |grep -v grep>/root/redispid.txt
[root@bogon ~]# cat redispid.txt root 69984 1 0 23:09 pts/1 00:00:00 src/redis-server 127.0.0.1:21242 root 69992 1 0 23:09 pts/1 00:00:00 src/redis-server 127.0.0.1:21272 root 69994 1 0 23:09 pts/1 00:00:00 src/redis-server 127.0.0.1:21222 root 69998 1 0 23:09 pts/1 00:00:00 src/redis-server 127.0.0.1:21302 root 70001 1 0 23:09 pts/1 00:00:00 src/redis-server 127.0.0.1:21262 root 70006 1 0 23:09 pts/1 00:00:00 src/redis-server 127.0.0.1:21252 root 70009 1 0 23:09 pts/1 00:00:00 src/redis-server 127.0.0.1:21322 root 70013 1 0 23:09 pts/1 00:00:00 src/redis-server 127.0.0.1:21342 root 70015 1 0 23:09 pts/1 00:00:00 src/redis-server 127.0.0.1:21282 root 70016 1 0 23:09 pts/1 00:00:00 src/redis-server 127.0.0.1:21352 root 70017 1 0 23:09 pts/1 00:00:00 src/redis-server 127.0.0.1:21292 root 70030 1 0 23:09 pts/1 00:00:00 src/redis-server 127.0.0.1:21312 root 70875 1 0 23:10 pts/1 00:00:01 src/redis-server 127.0.0.1:21325 root 70877 1 0 23:10 pts/1 00:00:01 src/redis-server 127.0.0.1:21355 root 70894 1 0 23:10 pts/1 00:00:01 src/redis-server 127.0.0.1:21315 root 70900 1 0 23:10 pts/1 00:00:01 src/redis-server 127.0.0.1:21215 root 70954 1 0 23:10 pts/1 00:00:00 src/redis-server 127.0.0.1:21235 root 71190 1 0 23:10 pts/1 00:00:00 src/redis-server 127.0.0.1:21357 root 71211 1 0 23:10 pts/1 00:00:00 src/redis-server 127.0.0.1:21297 root 71221 1 0 23:10 pts/1 00:00:00 src/redis-server 127.0.0.1:21246 root 71448 1 0 23:10 pts/1 00:00:00 src/redis-server 127.0.0.1:21238 root 71468 1 0 23:10 pts/1 00:00:00 src/redis-server 127.0.0.1:21359 root 71843 1 0 23:11 pts/1 00:00:00 src/redis-server 127.0.0.1:21275 root 71928 1 0 23:11 pts/1 00:00:01 src/redis-server 127.0.0.1:21229 root 73476 1 0 23:18 pts/1 00:00:00 src/redis-server 127.0.0.1:21365
2.获取pid列
[root@bogon ~]# awk '{print $2}' redispid.txt 69984 69992 69994 69998 70001 70006 70009 70013 70015 70016 70017 70030 70875 70877 70894 70900 70954 71190 71211 71221 71448 71468 71843 71928 73476
3.执行kill动作。可以看到,直接kill掉了
[root@bogon ~]# awk '{print $2}' redispid.txt |xargs kill -9 [root@bogon ~]# ps -ef|grep redis root 73607 33777 0 23:27 pts/3 00:00:00 grep --color=auto redis [root@bogon ~]# netstat -tlnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1003/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 34671/master tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 53270/zabbix_server tcp6 0 0 :::3306 :::* LISTEN 53233/mysqld tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::80 :::* LISTEN 54805/docker-proxy tcp6 0 0 :::22 :::* LISTEN 1003/sshd tcp6 0 0 ::1:25 :::* LISTEN 34671/master tcp6 0 0 :::10051 :::* LISTEN 53270/zabbix_server [root@bogon ~]#
4.简单解释一下xargs命令
格式如下:command |xargs command 第一个command是正常命令,获取到管道后需要用到的命令参数,第二个command是个不完成的命令,第二个命令的参数是第一个命令返回的结果。而xargs则是把管道前的参数传到管道后,同时把参数里包含的换行和空白全部替换为空格。所以才会出现文本里的多行pid被顺利kill掉。整个命令就相当于kill -9 69984 69992 69994 69998 70001 70006 70009 70013 70015 70016 70017 70030 70875 70877 70894 70900 70954 71190 71211 71221 71448 71468 71843 71928 73476