在Perl中运行system()命令时显示进度

joh*_*ohn 7 perl

我有一个执行某些任务的Perl脚本,其中一个是调用system命令"tar -cvf file.tar.....".

这通常需要一些时间,所以我希望命令行回显一个进度指示器,比如#system呼叫正在进行时回显到屏幕.

我一直在做一些挖掘并偶然发现fork.这是最好的方式吗?是否可以分叉system命令,然后创建一个while循环来检查$pidfork返回的状态?

我也看到过waitpid....我猜我也需要使用它.

fork system("tar ... ")
while ( forked process is still active) {
    print #
    sleep 1
}
Run Code Online (Sandbox Code Playgroud)

我吠叫错了树吗?

非常感谢约翰

Joh*_*han 7

Perl有一个很好的结构,称为"管道打开".您可以通过perldoc -f open在shell提示符下键入来阅读有关它的更多信息.

# Note the use of a list for passing the command. This avoids
# having to worry about shell quoting and related errors.
open(my $tar, '-|', 'tar', 'zxvf', 'test.tar.gz', '-C', 'wherever') or die ...;
Run Code Online (Sandbox Code Playgroud)

这是一个显示示例的片段:

  open(my $tar, '-|', 'tar', ...) or die "Could not run tar ... - $!";
  while (<$tar>) {
       print ".";
  }
  print "\n";
  close($tar);
Run Code Online (Sandbox Code Playgroud)

print "."每隔10到100行更换一次打印哈希标记的东西,以获得一个漂亮的标尺.

  • @john是的,只有在命令写入内容时它才有效.如果子进程没有写任何东西,那么你需要使用不同的结构,比如`fork` /`exec`/nonblocking`waitpid` /`sleep`.@Johan您的代码示例使用`$ | ++`会更好,以便立即打印点.:) (2认同)

hob*_*bbs 6

一个不依赖于子进程编写任何类型输出的示例,只要它正在运行就会打印一个大约一秒的点:

use POSIX qw(:sys_wait_h);
$|++;

defined(my $pid = fork) or die "Couldn't fork: $!";

if (!$pid) { # Child
  exec('long_running_command', @args) 
    or die "Couldn't exec: $!";
} else { # Parent
  while (! waitpid($pid, WNOHANG)) {
    print ".";
    sleep 1;
  }
  print "\n";
}
Run Code Online (Sandbox Code Playgroud)

虽然它可能有更多的错误检查,但CPAN上可能已经有了更好的东西.Proc :: Background似乎很有希望抽象出这种工作,但我不确定它是多么可靠.


Pet*_* G. 1

我会尝试这样的事情

open my $tar, "tar -cvf file.tar..... 2>&/dev/null |"
    or die "can't fork: $!";
my $i = 0;
while (<$tar>) {
    if( i++ % 1000 == 0 ) print;
} 
close $tar or die "tar error: $! $?";
Run Code Online (Sandbox Code Playgroud)

  • 请对文件/进程句柄使用本地词汇,而不是全局词汇。 (2认同)