3

Linux Fu: Making Progress

 1 year ago
source link: https://hackaday.com/2023/05/24/linux-fu-making-progress/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Linux Fu: Making Progress

382cd2e2ece26c65e289cf5970780cb6.gif

Skip to content

The computer world looks different from behind a TeleType or other hardcopy terminal. Things that tend to annoy people about Unix or Linux these days were perfectly great when you were printing everything the computer said to you. Consider the brevity of most basic commands. When you copy a file, for example, it doesn’t really tell you much other than it returns you to the prompt when it is done. If you are on a modern computer working with normal-sized files locally, not a big deal. But if you are over a slow network or with huge files, it would be nice to have a progress bar. Sure, you could write your own version of copy, but wouldn’t it be nice to have some more generic options?

One Way

The pv program can do some of the things you want. It monitors data through a pipe or, at least through its standard output. Think of it as cat with a meter. Suppose you want to write a diskimage to /dev/sdz:

cat diskz.img >/dev/sdz

But you could also do:

pv diskz.img >/dev/sdz

By default, pv will show a progress bar, an elapsed time, an estimated end time, a rate, and a total number of bytes. You can turn any of that off or add things using command line options. You can also specify things like the size of the terminal if it should count lines instead of bytes, and, in the case where the program doesn’t know what it is reading, the expected size of the transfer.

Of course, the output of pv is to stderr so it shows up on the screen. This makes it tricky if you want to use it with something like dialog that wants the stdout to be the progress indication. Luckily, you can redirect stderr to stdout and then redirect stdout to a file. Just be sure to do it in that order. For example:

pv -n /tmp/bigfile.iso 2>&1 >/tmp/bigfile.iso.backup | dialog --gauge "Copy Progress" 10 60

Another Way

There is also progress. It looks around for programs running like cp, mv, dd, and many more, looks at their open files, and shows you progress information for those programs. It only does this once, so you’ll typically marry it with the watch command or use the -M option. You can also monitor arbitrary programs with a little help. For example, suppose you have a big download in firefox:

watch progress -c firefox

Actually, if you are like me and have more than 32 instances of firefox running, that’s going to fail, but you get the idea.

The progress program can also take a pipe, but it still only monitors processes it knows about unless you use -c. You can add to the list with the -a option. For example:

tar czf /tmp/books.tgz ~/library/ebooks | progress -m

Results in:

prog.png?w=400A long tar command

Note that since gzip is one of the programs that progress knows about, it shows up on the list, too. You could, of course, tell it to only look for tar with -c.

As Usual

As usual with Linux, there are always many ways to do anything. If you are a masochist, you could use ptrace to look inside a program and figure things out from there. You could also do the same trick that progress does and look through a program’s /proc entries.

If you want to add a progress bar to your shell scripts directly, try gum. Or maybe combine progress with widgets to put the result on your desktop?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK