Linux——xargsコマンド学習
21564 ワード
指定したコマンドを結果に戻して処理する必要がある場合があります.
この場合、forループなどのスクリプトを書く必要があるかもしれません(現在はこの方法しか考えられません)
しかし、xargsコマンドがもう一つあることを思い出すと、このコマンドを組み合わせると手間が省けます.
シーンは次のとおりです.
Redisをインストールしてmake testを実行すると、Redisが実行中に競合が発生したことを報告します.ps-efはRedisを見て、本当に10人以上のredisプロセスが走っていることに気づいた.それは全部閉じるしかない.一つ一つkillが現実的ではないのでxargsコマンドを検討して実現した.以下は私のやり方です.
1.まず、すべてのredisプロセス情報を取得する必要があります.
ps -ef|grep redis |grep -v grep>/root/redispid.txt
2.pid列の取得
3.kill動作を実行します.直接killが落ちているのが見えます
4.xargsコマンドを簡単に説明する
フォーマットは、command|xargs commandの最初のcommandは通常のコマンドであり、パイプを取得した後に使用するコマンドパラメータであり、2番目のcommandは完了しないコマンドであり、2番目のコマンドのパラメータは最初のコマンドが返された結果である.xargsは,パイプ前のパラメータをパイプに伝達するとともに,パラメータに含まれる改行と空白をすべてスペースに置き換える.テキストの複数行pidがスムーズにkillされるようになります.命令全体はkill-9 69984 69992 69994 69998 70001 70006 70009 70013 70015 70016 70016 70016 70016 70016 70017 70030 70877 70894 70900 70954 71190 71211 7122171448 71468 71843 71928 73476に相当する
この場合、forループなどのスクリプトを書く必要があるかもしれません(現在はこの方法しか考えられません)
しかし、xargsコマンドがもう一つあることを思い出すと、このコマンドを組み合わせると手間が省けます.
シーンは次のとおりです.
Redisをインストールしてmake testを実行すると、Redisが実行中に競合が発生したことを報告します.ps-efはRedisを見て、本当に10人以上の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は通常のコマンドであり、パイプを取得した後に使用するコマンドパラメータであり、2番目のcommandは完了しないコマンドであり、2番目のコマンドのパラメータは最初のコマンドが返された結果である.xargsは,パイプ前のパラメータをパイプに伝達するとともに,パラメータに含まれる改行と空白をすべてスペースに置き換える.テキストの複数行pidがスムーズにkillされるようになります.命令全体はkill-9 69984 69992 69994 69998 70001 70006 70009 70013 70015 70016 70016 70016 70016 70016 70017 70030 70877 70894 70900 70954 71190 71211 7122171448 71468 71843 71928 73476に相当する