Linux - xargs command learning

Sometimes we encounter a situation you need to specify command returns the result of processing

In this case, you may need to write a script for processing loop like the (I can only think of this approach)

But think of it there is a xargs command, this command is a combination of the relatively easy.

The scenario:

When installing Redis execution make test, Redis has been reported to lead to conflict in the run. ps -ef see Redis, really found a dozen redis process run again. I can only put it all closed up. One by one, kill unrealistic, so study a little xargs command. Here is what I practice

1. First of all we need to get to redis process information

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. Get the pid column

[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. Perform kill action. It can be seen directly kill off

[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. briefly explain xargs command

The following format: command | xargs command the first command is a normal command, command parameters to obtain the need to use the conduit, the second command is not a complete command, the second command parameter is the result returned by the first command . The xargs is the parameter passed before the pipeline after the pipeline, while the parameter contains line breaks and replace all blank spaces. Thus giving rise to multiple lines of text in the pid was successfully kill off. The entire command is equivalent to 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

 

Guess you like

Origin www.cnblogs.com/biaopei/p/11691169.html