LoginSignup
4
7

More than 3 years have passed since last update.

Linuxのパイプの流れの進捗を見守るには

Last updated at Posted at 2019-10-21

時間のかかるコマンドをパイプをつないで実行したときに、パイプを流れるデータ量を進捗表示してくれると、ちゃんと動いていることが確認でき、残り時間も想像がつくので安心です。処理速度もリアルタイムにわかります。

そんなときはpvというコマンドを使います。

使用例

こんな感じで使えます。

$ COMMAND1 | pv | COMMAND2
10.1GiB 0:12:38 [9.92MiB/s] [                    <=>                            ]

左から、流れたデータ量、コマンドが始まってからの時間、データの流れる平均速度が数値で表示されます。

2か所以上のパイプを監視したい場合は次のように-c--nameを使います。

$ COMMAND1 | pv -c --name 1 | COMMAND2 | pv -c --name 2 | COMMAND3 | pv -c --name 3 | COMMAND4
        1: 22.2MiB 0:00:03 [6.78MiB/s] [  <=>                                   ]
        3: 1016KiB 0:00:03 [ 406KiB/s] [                   <=>                  ]
        2: 1.09MiB 0:00:03 [ 388KiB/s] [                   <=>                  ]

-cはpvを複数同時に使うときにそれぞれが端末上でちゃんと表示されるようにするために必須です。

--name <NAME>を指定すると進捗表示の左にラベルとして表示してくれるので、pvを複数同時に使うときには付けたほうが見やすいです。付けないとどの進捗表示がどのパイプのことなのかがわからないです。上の例のように順番通りにならない場合もあります。

pvはcatコマンドと同等に標準入力をそのまま標準出力するので、以下のようにリダイレクトもできます。入力の全体のデータ量がわかる場合は、進捗のパーセント表示も右端に表示されます。

$ pv -c --name in < input.txt | COMMAND2 | pv -c --name out > output.txt
       in:  949MiB 0:00:11 [85.2MiB/s] [==================>     ] 60% ETA 0:00:07
      out: 48.2MiB 0:00:11 [4.41MiB/s] [           <=>                          ]

インストール

CentOS

CentOSにはyumにはないようでして、rpmを直接インストールします。

rpmのURLはpv公式サイト
http://www.ivarch.com/programs/pv.shtml
で確認できます。最新バージョンのrpmのURLをここで確認してください。

$ sudo rpm -ivh http://www.ivarch.com/programs/rpms/pv-1.6.6-1.x86_64.rpm
...
$ pv --version
pv 1.6.6 - Copyright 2015 Andrew Wood <andrew.wood@ivarch.com>

Web site: http://www.ivarch.com/programs/pv.shtml

This program is free software, and is being distributed under the
terms of the Artistic License 2.0.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

2019年10月現在、CentOS 7で確認しました。
2021年1月現在、CentOS 8でも確認しました。

Ubuntu

Ubuntuにはaptでインストールできます。

$ sudo apt update
...
$ sudo apt install pv
...
$ pv --version
pv 1.6.6 - Copyright 2015 Andrew Wood <andrew.wood@ivarch.com>

Web site: http://www.ivarch.com/programs/pv.shtml

This program is free software, and is being distributed under the
terms of the Artistic License 2.0.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

2019年10月現在、Ubuntu 18.04で確認しました。
2021年1月現在、Ubuntu 20.04でも確認しました。

オプション

$ pv --help
Usage: pv [OPTION] [FILE]...
Concatenate FILE(s), or standard input, to standard output,
with monitoring.

  -p, --progress           show progress bar
  -t, --timer              show elapsed time
  -e, --eta                show estimated time of arrival (completion)
  -I, --fineta             show absolute estimated time of arrival
                           (completion)
  -r, --rate               show data transfer rate counter
  -a, --average-rate       show data transfer average rate counter
  -b, --bytes              show number of bytes transferred
  -T, --buffer-percent     show percentage of transfer buffer in use
  -A, --last-written NUM   show NUM bytes last written
  -F, --format FORMAT      set output format to FORMAT
  -n, --numeric            output percentages, not visual information
  -q, --quiet              do not output any transfer information at all

  -W, --wait               display nothing until first byte transferred
  -D, --delay-start SEC    display nothing until SEC seconds have passed
  -s, --size SIZE          set estimated data size to SIZE bytes
  -l, --line-mode          count lines instead of bytes
  -0, --null               lines are null-terminated
  -i, --interval SEC       update every SEC seconds
  -w, --width WIDTH        assume terminal is WIDTH characters wide
  -H, --height HEIGHT      assume terminal is HEIGHT rows high
  -N, --name NAME          prefix visual information with NAME
  -f, --force              output even if standard error is not a terminal
  -c, --cursor             use cursor positioning escape sequences

  -L, --rate-limit RATE    limit transfer to RATE bytes per second
  -B, --buffer-size BYTES  use a buffer size of BYTES
  -C, --no-splice          never use splice(), always use read/write
  -E, --skip-errors        skip read errors in input
  -S, --stop-at-size       stop after --size bytes have been transferred
  -R, --remote PID         update settings of process PID

  -P, --pidfile FILE       save process ID in FILE

  -d, --watchfd PID[:FD]   watch file FD opened by process PID

  -h, --help               show this help and exit
  -V, --version            show version information and exit

Please report any bugs to <pv@ivarch.com>.

関連

-Lオプションでパイプの流速を制限できます。

Linuxのパイプの流れる速度を制限するには

4
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7