Linux——xargs命令学习

有时候我们会遇到需要将指定命令返回结果进行处理的情况

这种情况下,可能就需要写for循环之类的脚本进行处理了(目前我只能想到这种方法)

但是想起来还有一个xargs命令,组合这个命令就比较省事了。

场景如下:

安装Redis执行make test时,报Redis已在运行导致冲突。ps -ef查看Redis,发现还真的有十来个redis进程再跑。那就只能把其全部关闭了。一个一个kill不现实,于是研究了一下xargs命令来实现。以下是我的做法

1.首先我们需要获取到所有redis进程信息

ps -ef|grep redis |grep -v grep>/root/redispid.txt

[[email protected] ~]# 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列

[[email protected] ~]# 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掉了

[[email protected] ~]# awk ‘{print $2}‘ redispid.txt |xargs kill -9
[[email protected] ~]# ps -ef|grep redis
root      73607  33777  0 23:27 pts/3    00:00:00 grep --color=auto redis
[[email protected] ~]# 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
[[email protected] ~]# 

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

原文地址:https://www.cnblogs.com/biaopei/p/11691169.html

时间: 2024-08-30 06:03:05

Linux——xargs命令学习的相关文章

linux shell 命令学习(5) xxd- make a hexdump or do the reverse.

对于标准输入或者给定的文件,显示其16进制的内容.也可以反过来进行转换. ? 1 2 3 xxd -h[elp] xxd [options] [infile [outfile]] xxd -r[evert] [options] [infile [outfile]] 如果没有指定输入文件, 则采用标准输入. -b: 以2进制格式进行输出 ? 1 2 3 4 [[email protected] src]$ xxd -b train.ini 0000000: 01011011 01110100 01

Linux xargs命令,-print0,xargs -0的应用

xargs是一个过滤器,可以给命令传递参数;也是组合多个命令的一个工具,它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理. 通常情况下,xargs从管道或者stdin中读取数据,然而它也能够从文件的输出中读取数据. xargs的默认命令是echo,这意味着通过管道传递给xargs的输入将会包含换行和空白,不过通过xargs的处理,换行和空白将被空格取代. xargs是一个强有力的命令,它能够捕获一个命令的输出,然后传递给另外一个命令,下面是一些如何有效使用xargs 的实用例子.

linux基础命令学习(六)DHCP服务器配置

工作原理:        1.客户机寻找服务器:广播发送discover包,寻找dhcp服务器        2.服务器响应请求:单播发送offer包,对客户机做出响应.提供客户端网络相关的租约以供选择        其中服务器在收到客户端的请求后,会针对客户端的mac地址与本身的设定数据进行一下工作:            a.到服务器的登录文件中寻找该用户之前曾经使用过的ip,若有且该ip目前没有人使用,这提供此ip为客户机            b.若配置文件中有针对该mac提供额外的固定

linux基础命令学习(七)samba服务器配置

samba有五种安全级别,它们分别是: share:不需要samba账户就可登陆samba服务器      user:需要添加samba账户才可以登陆samba服务器      server:由另外一台samba服务器来对用户进行身份验证.       domain:把samba服务器加入到NT域,由NT的域控制器来进行身份验证.      ADS:Active Directory Service,活动目录服务,它是samba3.0中新增的身份验证方式.采用ADS验证方式,samba服务器集成到

张明贵-Linux基础命令学习-5

[让总结成为一种习惯] pwd   打印当前工作目录 print working directory pwd -L pwd的默认参数是-L,执行pwd和执行pwd -L结果是相同的,其实使用的是环境变量 PWD echo $PWD pwd pwd -L pwd -P 打印物理目录,没有任何符号链接 当我们切换到 /etc/init.d目录下 实际上和切换到 /etc/rc.d/init.d  目录下是一样的 执行效果一样 mkdir  创建目录 make directory mkdir -p /

Linux常用命令学习

补充: 管道符号:   | 含义: 命令1 的正确输出作为命令2的输出对象. 格式: 命令1   |  命令2 举例: ls -ctrl |  more 常用命令: netstat   -an    |  grep    ESTABLISHED         查看正在连接的端口 netstat   -an    |   grep   LISTEN find   .    -name   test.txt    |     cat    -n          在当前目录下找到文件名为test.

Linux xargs命令

之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了xargs命令,xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具.它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理.通常情况下,xargs从管道或者stdin中读取数据,但是它也能够从文件的输出中读取数据.xargs的默认命令是echo,这意味着通过管道传递给xargs的输入将会包含换行和空白,不过通过xargs的处理,换行和空白将被空格取代. xargs 是一个强

linux基础命令学习(六)文件的特殊属性

Linux chattr命令用于改变文件属性. 这项指令可改变存放在ext2文件系统上的文件或目录属性,这些属性共有以下8种模式: a:让文件或目录仅供附加用途.    b:不更新文件或目录的最后存取时间.    c:将文件或目录压缩后存放.    d:将文件或目录排除在倾倒操作之外.    i:不得任意更动文件或目录.    s:保密性删除文件或目录.    S:即时更新文件或目录.    u:预防以外删除. 语法: chattr [-RV][-v<版本编号>][+/-/=<属性>

(4)Linux常用命令学习

Linux常用命令 一.命令基本格式 命令 [选项]  [参数] 注意:个别命令使用不遵循此格式,当有多个选项时,可以写在一起.简化选项与完整选项 -a 等于 --all [[email protected] ~]# 其中: root:            当前登录用户 localhost:             主机名 ~                          当前所在目录(家目录) #                          超级用户的提示符 普通用户的提示符是$