LINUX


Monday 29 July 2013

Linux / Unix pv Command: Monitor Progress of Data Sent Via a Pipe

Linux / Unix pv Command: Monitor Progress of Data Sent Via a Pipe

The pv command allows you to see the progress of data through a pipeline. It provides the following info:
  1. Time elapsed
  2. Percentage completed (with progress bar)
  3. Current throughput rate
  4. Total data transferred
  5. ETA

From the project home page:
It can be inserted into any normal pipeline between two processes to give a visual indication of how quickly data is passing through, how long it has taken, how near to completion it is, and an estimate of how long it will be until completion.

How do I install pv command?

By default pv command is not installed. Type the following apt-get command under Debian / Ubuntu Linux:
# apt-get install pv
Sample outputs:
 
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
  doc-base
The following NEW packages will be installed:
  pv
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 28.9 kB of archives.
After this operation, 143 kB of additional disk space will be used.
Get:1 http://mirror.anl.gov/debian/ squeeze/main pv amd64 1.1.4-1 [28.9 kB]
Fetched 28.9 kB in 1s (16.1 kB/s)
Selecting previously deselected package pv.
(Reading database ... 32240 files and directories currently installed.)
Unpacking pv (from .../archives/pv_1.1.4-1_amd64.deb) ...
Processing triggers for man-db ...
Setting up pv (1.1.4-1) .
 
RHEL / CentOS / SL / Fedora Linux users, turn on EPEL repo and type the following yum command to install pv:
# yum install pv
Sample outputs:
 
Loaded plugins: product-id, protectbase, rhnplugin, subscription-manager
Updating certificate-based repositories.
Unable to read consumer identity
0 packages excluded due to repository protections
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package pv.x86_64 0:1.1.4-3.el6 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
====================================================================================================
 Package            Arch                   Version                       Repository            Size
====================================================================================================
Installing:
 pv                 x86_64                 1.1.4-3.el6                   epel                  34 k
 
Transaction Summary
====================================================================================================
Install       1 Package(s)
 
Total download size: 34 k
Installed size: 67 k
Is this ok [y/N]: y
Downloading Packages:
pv-1.1.4-3.el6.x86_64.rpm                                                    |  34 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : pv-1.1.4-3.el6.x86_64                                                            1/1
Installed products updated.
  Verifying  : pv-1.1.4-3.el6.x86_64                                                            1/1
 
Installed:
  pv.x86_64 0:1.1.4-3.el6
 
Complete!
 
FreeBSD users can use the port to install pv, enter:
# cd /usr/ports/sysutils/pv/
# make install clean

OR add the binary package, run:
# pkg_add -r pv

pv Command syntax

The syntax is
 
pv filename
pv filename > /path/to/output
pv options filename | command1
pv options filename | command1 > output.file
pv filename | command1
command1 | pv | command2
pv -options input.file | command1 | pv -options > output.file
 

pv command Examples

In this example copy a file called origin-cdn.cyberciti.org_access.log to /tmp/origin-cdn-access.log and show progress:
# pv origin-cdn.cyberciti.org_access.log > /tmp/origin-cdn-access.log
OR just send it to /dev/null:
# pv origin-cdn.cyberciti.org_access.log > /dev/null
1.41GB 0:00:21 [66.3MB/s] [=====================================================>] 100%
Use nc to create a network port # 2000. Type the following command:
$ nc -l -v -w 30 -p 2000 > /tmp/data.bin
Open another terminal and type:
$ pv firmware33921.bin | nc -w 1 127.0.0.1 2000
Sample outputs:
Fig.01: pv command in action
Fig.01: pv command in action

In this example you can see progress of both pipes:
 
pv -cN rawlogfile origin-cdn.cyberciti.org_access.log | gzip | pv -cN gziplogfile > access.log.gz
 
Sample outputs:
Fig.02: pv displaying output of complicated pipes and commands
Fig.02: pv displaying output of complicated pipes and commands

Where,
  • -c : Use cursor positioning escape sequences instead of just using carriage returns. This is useful in conjunction with -N (name) if you are using multiple pv invocations in a single, long, pipeline.
  • N rawlogfile : Prefix the output information with NAME. Useful in conjunction with -c if you have a complicated pipeline and you want to be able to tell different parts of it apart.

pv and dialog command

You can create a progress bar (progress indicator) when copying/moving/extracting files or making backups using the gauge box:
 
echo percentage | dialog --gauge "text" height width percent
echo "10" | dialog --gauge "Please wait" 10 70 0
echo "50" | dialog --gauge "Please wait" 10 70 0
echo "100" | dialog --gauge "Please wait" 10 70 0
 
In this example, extract tar ball and show progress using the dialog command:
 
(pv -n backup.tar.gz | tar xzf - -C path/to/data ) 2>&1 | dialog --gauge "Running tar, please wait..." 10 70 0
 
Sample outputs:
Fig.03: pv and dialog command in action
Fig.03: pv and dialog command in action
Finally, you can always use the rsync command to display progress bar for both local and remote files.

No comments:

Post a Comment