GNU parallel可以并行地执行shell
命令。看一个简单的例子:
# ls
a b c
# cat a
aaaa
# cat b
bbbb
# cat c
cccc
# parallel cat ::: *
aaaa
bbbb
cccc
上面的例子输出了a
,b
和c
3个文件的内容。:::
告诉GNU parallel
从命令行而不是stdin
读取参数,而shell
会把*
扩展成当前目录下的文件名。
再看一个例子:
# parallel sleep {}\; echo {} ::: 2 1 4 3
1
2
3
4
# parallel -k sleep {}\; echo {} ::: 2 1 4 3
2
1
4
3
正常情况下,完成一个job
,parallel
就会把这个job
的内容输出。-k
选项保证输出顺序和输入顺序一致。而{}
会替换成input line
。
GNU parallel
不会把多个输出的内容混杂在一起,对比下列两个命令输出:
# traceroute foss.org.my & traceroute debian.org & traceroute freenetproject.org & wait
[1] 4920
[2] 4921
[3] 4922
traceroute to debian.org (149.20.20.20), 30 hops max, 60 byte packets
traceroute to freenetproject.org (80.68.94.117), 30 hops max, 60 byte packets
foss.org.my: Name or service not known
Cannot handle "host" cmdline arg `foss.org.my' on position 1 (argc 1)
[1] Exit 2 traceroute foss.org.my
1 16.187.248.2 (16.187.248.2) 1.705 ms 1.709 ms 2.067 ms
1 16.187.248.2 (16.187.248.2) 1.315 ms 1.314 ms 1.618 ms
......
# parallel traceroute ::: foss.org.my debian.org freenetproject.org
foss.org.my: Name or service not known
Cannot handle "host" cmdline arg `foss.org.my' on position 1 (argc 1)
traceroute to debian.org (140.211.15.34), 30 hops max, 60 byte packets
1 16.187.248.2 (16.187.248.2) 1.871 ms 1.857 ms 2.151 ms
......
traceroute to freenetproject.org (80.68.94.117), 30 hops max, 60 byte packets
1 16.187.248.2 (16.187.248.2) 2.175 ms 2.471 ms 2.471 ms
2 16.160.221.81 (16.160.221.81) 0.456 ms 0.463 ms 0.463 ms
......
参考资料:
GNU Parallel: The Command-Line Power Tool;
GNU Parallel manual。