xargsライクにパラレルでコマンドを実行させる『machma』

ネットで調べ物中に、xargsみたいな使い方でパラレルでコマンドを実行させることができる『machma』なるコマンドツールを見かけたのでお試し。 GNU拡張されたxargsの場合、並列でコマンドを実行させることができるのだが、実行結果の出力時に使用したパラメータを先頭に入れて表示してくれたりするので、使い勝手はこちらのほうが良いと思う。

Golangで記述されているので、まだGoのコンパイル環境がない場合は別途用意する必要がある。 環境が用意できたら、以下のコマンドでインストールを行う。

go get github.com/fd0/machma
go install github.com/fd0/machma

インストールができたら、実際に利用してみよう。 受付した値は「{}」で表現されるので、実行するコマンドで受け付けた値を差し込む箇所に入れてやる。 とりあえず、サンプルにあるようにpingを実行してみる。

echo 172.20.100.1{18..21} | tr ' ' '\n' | machma -- sh -c 'ping -c 2 {}'
blacknon@BS-PUB-DEVELOP:~$ echo 172.20.100.1{18..21} | tr ' ' '\n' | machma -- sh -c 'ping -c 2 {}'
1 2017-05-30 09:03:42 172.20.100.118 PING 172.20.100.118 (172.20.100.118) 56(84) bytes of data.
1 2017-05-30 09:03:42 172.20.100.118 64 bytes from 172.20.100.118: icmp_seq=1 ttl=64 time=0.566 ms
2 2017-05-30 09:03:42 172.20.100.119 PING 172.20.100.119 (172.20.100.119) 56(84) bytes of data.
2 2017-05-30 09:03:42 172.20.100.119 64 bytes from 172.20.100.119: icmp_seq=1 ttl=64 time=0.638 ms
2 2017-05-30 09:03:43 172.20.100.119 64 bytes from 172.20.100.119: icmp_seq=2 ttl=64 time=0.617 ms
2 2017-05-30 09:03:43 172.20.100.119 --- 172.20.100.119 ping statistics ---
2 2017-05-30 09:03:43 172.20.100.119 2 packets transmitted, 2 received, 0% packet loss, time 999ms
2 2017-05-30 09:03:43 172.20.100.119 rtt min/avg/max/mdev = 0.617/0.627/0.638/0.027 ms
1 2017-05-30 09:03:43 172.20.100.118 64 bytes from 172.20.100.118: icmp_seq=2 ttl=64 time=0.463 ms
1 2017-05-30 09:03:43 172.20.100.118 --- 172.20.100.118 ping statistics ---
1 2017-05-30 09:03:43 172.20.100.118 2 packets transmitted, 2 received, 0% packet loss, time 1001ms
1 2017-05-30 09:03:43 172.20.100.118 rtt min/avg/max/mdev = 0.463/0.514/0.566/0.056 ms
4 2017-05-30 09:03:43 172.20.100.121 PING 172.20.100.121 (172.20.100.121) 56(84) bytes of data.
4 2017-05-30 09:03:43 172.20.100.121 64 bytes from 172.20.100.121: icmp_seq=1 ttl=64 time=0.515 ms
3 2017-05-30 09:03:43 172.20.100.120 PING 172.20.100.120 (172.20.100.120) 56(84) bytes of data.
3 2017-05-30 09:03:43 172.20.100.120 64 bytes from 172.20.100.120: icmp_seq=1 ttl=64 time=0.579 ms
4 2017-05-30 09:03:44 172.20.100.121 64 bytes from 172.20.100.121: icmp_seq=2 ttl=64 time=0.635 ms
4 2017-05-30 09:03:44 172.20.100.121 --- 172.20.100.121 ping statistics ---
4 2017-05-30 09:03:44 172.20.100.121 2 packets transmitted, 2 received, 0% packet loss, time 1001ms
4 2017-05-30 09:03:44 172.20.100.121 rtt min/avg/max/mdev = 0.515/0.575/0.635/0.060 ms
3 2017-05-30 09:03:44 172.20.100.120 64 bytes from 172.20.100.120: icmp_seq=2 ttl=64 time=0.559 ms
3 2017-05-30 09:03:44 172.20.100.120 --- 172.20.100.120 ping statistics ---
3 2017-05-30 09:03:44 172.20.100.120 2 packets transmitted, 2 received, 0% packet loss, time 1000ms
3 2017-05-30 09:03:44 172.20.100.120 rtt min/avg/max/mdev = 0.559/0.569/0.579/0.010 ms

processed 4 items (0 failures) in 0:02

タイムスタンプも出力してくれるし、受け付けたパラメータについてもちゃんと行ごとに出力してくれるので、かなり見やすい。 こりゃ便利だ。