如何输出只有最新的屏幕与php执行


How to output only the latest screen with php exec?

我的问题是:

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

我现在是这样做的:

$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命令附加了输出文本,而不是重写它。我想重写一下

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

考虑到这一点,你剩下两个选项:

  • 使用tail将输出限制为9行,然后将输出发送到文件。您的错误输出将需要重定向到一个单独的文件,并根据需要回读。但是,由于$!less的pid,因此pid丢失了。

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

    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读取,并继续读取$tdir/fifo中的最后9行,直到进程写入fifo完成,然后将它们写入重定向到$outputfile的标准输出。现在,由于$tdir/fifo是fifo,因此没有使用磁盘空间。