bash – xargs:在并行模式下将stdout重定向到文件时丢失输出
发布时间:2020-12-15 19:02:38 所属栏目:安全 来源:网络整理
导读:我在并行模式下使用GNU xargs(版本4.2.2),当重定向到文件时,我似乎可靠地丢失输出.重定向到管道时,它似乎正常工作. 以下shell命令演示了该问题的minimum,complete,and verifiable example.我使用xargs生成2550个数字,将其分成100个args的行,每行总共26行,其
我在并行模式下使用GNU xargs(版本4.2.2),当重定向到文件时,我似乎可靠地丢失输出.重定向到管道时,它似乎正常工作.
以下shell命令演示了该问题的minimum,complete,and verifiable example.我使用xargs生成2550个数字,将其分成100个args的行,每行总共26行,其中第26行只包含50个args. # generate numbers 1 to 2550 where each number is on its own line $seq 1 2550 > /tmp/nums $wc -l /tmp/nums 2550 /tmp/nums # piping to wc is accurate: 26 lines,2550 args $xargs -P20 -n 100 </tmp/nums | wc 26 2550 11643 # redirecting to a file is clearly inaccurate: 22 lines,2150 args $xargs -P20 -n 100 </tmp/nums >/tmp/out; wc /tmp/out 22 2150 10043 /tmp/out 我认为问题与底层shell无关,因为shell将在命令执行之前执行重定向并等待xargs完成.在这种情况下,我假设xargs在刷新缓冲区之前完成.但是,如果我的假设是正确的,我不知道为什么在写入管道时这个问题不会出现. 编辑: 使用>>时出现(创建/附加到文件)在shell中,问题似乎没有表现出来: # appending to file $>/tmp/out $xargs -P20 -n 100 </tmp/nums >>/tmp/out; wc /tmp/out 26 2550 11643 # creating and appending to file $rm /tmp/out $xargs -P20 -n 100 </tmp/nums >>/tmp/out; wc /tmp/out 26 2550 11643
您的问题是由于不同进程的输出混合在一起.它显示在这里:
parallel perl -e '$a="1{}"x10000000;print $a,"n"' '>' {} ::: a b c d e f ls -l a b c d e f parallel -kP4 -n1 grep 1 > out.par ::: a b c d e f echo a b c d e f | xargs -P4 -n1 grep 1 > out.xargs-unbuf echo a b c d e f | xargs -P4 -n1 grep --line-buffered 1 > out.xargs-linebuf echo a b c d e f | xargs -n1 grep 1 > out.xargs-serial ls -l out* md5sum out* 解决方案是缓冲每个作业的输出 – 在内存中或在tmpfiles中(如GNU Parallel). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |