3

有什么方法可以跟踪或监控进度xargs吗?

我正在用 for performance 替换for循环find … | xargs(特别是并行运行任务)。我读过它parallel有一个进度标志,但我想知道是否有办法严格使用xargs.

我也知道并行运行的任务xargs不一定会以正确的顺序完成,这增加了监控进度的复杂性。即使一个解决方案能让我大致了解进展情况,那也是一个很好的开始。

4

3 回答 3

3

If you just want to input how many lines you roughly already processed you may create simple shell function to do that

#!/bin/bash

#-----
##
## @function count
##
## @desc Write every n'th number (if n is 5 write 5, 10, 15, ...)
##
## @param $1 - number 
##
#-----
function count {
   typeset C=0
   while read L; do
      C=$(( C + 1 ))
      if [ $(( $C % $1 )) -eq 0 ]; then
         echo $C 1>&2
      fi
      echo "$L"
   done
}

find . | count 100 | xargs ...

Small problem is that this prints number of lines passed to xargs, not number of lines already processed by the command invoked by xargs. Plus every pipe has some buffer, so it will show slightly higher number than it should. On my machine it showed ~500 lines in advance to real state but if you are processing sufficiently large number of lines 500 is negligible :)

于 2013-05-10T21:02:22.850 回答
2

如果您只是在寻找进度的一般指示,最简单的方法是在执行您想要执行的命令之前回显。

例子: cat <someinput> | xargs -I{} sh -c 'echo {}; <somecmd>;'

-I{}设置{}为当前正在处理的字符串

sh -c将允许您执行多个命令(注意:每个命令后都需要分号,包括最后一个.

于 2017-01-23T03:54:24.553 回答
0

您可以使用pv来监视管道(虽然不是文件,因为它位于您的管道中)但是您必须告诉pv查找输出的大小,这看起来非常麻烦(请参阅此答案)。

我真的建议使用parallel,根据您要完成的任务……这正是它的设计目的。

否则,如果您安装了 Apple Developer Tools,您可以编写一个生成 Makefile 的小脚本来完成相同的任务,并确保它以您想要的方式打印进度。

于 2013-04-03T17:36:19.657 回答