0

我的问题是:

我有使用 exec 命令生成的大型输出文件。我有大约 800-1500 MB 的文本输出,因为它每秒都附加到我的文本文件中。我怎么能只将最后一条数据写入我的文本文件?

这就是我现在的做法:

$cmd = 'btdownloadheadless --saveas /var/www/virtual/tigyisolutions.hu/boxy/htdocs/downloaded_torrent/'.$kovNev.'/ '.$_REQUEST["torrent"];
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));

我想在我的输出文件中看到这个:

saving:         Test torrent (1115.9 MB)
percent done:   19.8
time left:      22 min 04 sec
download to:    /var/www/virtual/tigyisolutions.hu/boxy/htdocs/downloaded_torrent/uid1_fil_1370552248/
download rate:  1344.1 kB/s
upload rate:    115.7 kB/s
share rating:   0.121  (26.8 MB up / 221.3 MB down)
seed status:    81 seen now, plus 3.994 distributed copies
peer status:    18 seen now, 45.3% done at 2175.4 kB/s

而不是这个:

saving:         Test torrent (1115.9 MB)
percent done:   19.8
time left:      22 min 04 sec
download to:    /var/www/virtual/tigyisolutions.hu/boxy/htdocs/downloaded_torrent/uid1_fil_1370552248/
download rate:  1344.1 kB/s
upload rate:    115.7 kB/s
share rating:   0.121  (26.8 MB up / 221.3 MB down)
seed status:    81 seen now, plus 3.994 distributed copies
peer status:    18 seen now, 45.3% done at 2175.4 kB/s
saving:         Test torrent (1115.9 MB)
percent done:   19.8
time left:      22 min 04 sec
download to:    /var/www/virtual/tigyisolutions.hu/boxy/htdocs/downloaded_torrent/uid1_fil_1370552248/
download rate:  1344.1 kB/s
upload rate:    115.7 kB/s
share rating:   0.121  (26.8 MB up / 221.3 MB down)
seed status:    81 seen now, plus 3.994 distributed copies
peer status:    18 seen now, 45.3% done at 2175.4 kB/s
saving:         Test torrent (1115.9 MB)
percent done:   19.8
time left:      22 min 04 sec
download to:    /var/www/virtual/tigyisolutions.hu/boxy/htdocs/downloaded_torrent/uid1_fil_1370552248/
download rate:  1344.1 kB/s
upload rate:    115.7 kB/s
share rating:   0.121  (26.8 MB up / 221.3 MB down)
seed status:    81 seen now, plus 3.994 distributed copies
peer status:    18 seen now, 45.3% done at 2175.4 kB/s ...etc...

所以我只想看到最新的屏幕。我的 bash 命令附加输出 txt 而不是重写它。我想重写它。

4

1 回答 1

0

你的问题很简单。下载程序通常会在屏幕上显示状态栏。该状态栏的绘制方式是清除屏幕上的旧状态栏,并在其顶部显示新的状态栏。然而,如果输出被重定向到一个文件,屏幕空白和新的输出不断被写入输出文件,使它的大小真的很大。

考虑到这一点,您有两个选择:

  • 用于tail在将输出发送到文件之前将输出限制为 9 行。您的错误输出将需要重定向到单独的文件并根据需要读回。但是, pid 和 的 pid 一样丢失$!less

    exec(sprintf("%s 2>error_file | tail -n 9 > %s & echo $! >> %s", $cmd, $outputfile, $pidfile));
    
  • 使用 mkfifo 创建管道,将输出写入管道并在管道的另一端使用 tail 写入输出文件。这会使命令复杂化为完整的小脚本

    exec(sprintf("tdir=`mktemp -d`; mkfifo $tdir/fifo; %s >$tdir/fifo 2>&1 & echo $! >> %s & tail -n 9 $tdir/fifo > %s &", $cmd, $pidfile, $outputfile));
    

为了解释它,mktemp -d创建一个临时目录并返回它的名称(存储在 中tdir)。$tdir/fifo是为 fifo 选择的名称,并且在给定 的属性的情况下保证是唯一的mktemp。命令输出发送到 fifo,pid 存储在pidfile. 但是,要写入outputfile,我们使用tail -n 9 $tdir/fifo读取$tdir/fifo并继续读取 up 中的最后 9 行,$tdir/fifo直到写入 fifo 的过程完成,然后将它们写入重定向到的标准输出$outputfile。现在作为$tdir/fifo先进先出,不使用磁盘空间。

于 2013-06-06T21:41:44.563 回答