Better progress and ETA using the pv command
Sometimes we need to run a task that takes a lot of time to finish. Since version 1.5.1, the pv command [1][2] has this neat option pv -d $PID
that will monitor open file descriptors of the process $PID
.
The pv command can detect when a process is reading a file from beginning to end, and it will kindly print transfer rate, ETA and a progress bar.
For example, We have a large CSV file that we want to load into a SQLite table.
➜ ~ ls -lh my.csv
-rw-r--r-- 1 assafmo assafmo 16G Aug 2 08:41 my.csv
➜ ~ sqlite3 my.db
SQLite version 3.16.2 2017-01-06 16:32:41
Enter ".help" for usage hints.
sqlite> .mode csv
sqlite> .import ./my.csv my_table
This process can take a lot of time and we get no indication from sqlite3 when it will finish.
We need to find out what process preforms the reading and get its PID. This can be cat, sed, awk and many others, but in our case it’s sqlite3.
Using pv -d $(pidof sqlite3)
gives us a nice progress bar and ETA for sqlite3 reading our file.
➜ ~ pv -d $(pidof sqlite3)
3:/home/assafmo/my.db: 0 B 0:00:07 [ 0 B/s] [<=> ]
4:/home/assafmo/my.csv: 152MiB 0:00:07 [1.96MiB/s] [> ] 0% ETA 2:14:37
5:/home/assafmo/my.db-journal: 0 B 0:00:07 [ 0 B/s] [<=> ]
Similarly, we can get progress and ETA when copying files with rsync -P -h
instead of cp
.