Resource friendly way to indicate to user program is not hanging

outputperl

I have a Perl script on a *nix system which, at one point, is processing 50,000 + lines of text.
This takes some time. I am trying to find a resource friendly way to let the user know the program is not hanging while it is processing all of this text.

Currently I am printing output in realtime as the text is being processed .
I am flushing the output buffer then printing the output on one line with \r. This seems to be an unnecessary use of resources because it takes almost twice as long than when I print nothing, but as I have said, when printing nothing it looks like the program is hanging.

So my question: Is there a standard or simple way to let the user know the program is indeed running while performing long running tasks?

Best Answer

[I just realized your script is perl, but the same logic applies, print "\r", etc. You will want to use STDERR or else turn off buffering, $| = 1. See bottom.]

One way of implementing a CLI "progress indicator" involves the use of the \r (carriage return) character. This brings the cursor to the beginning of the current line:

#!/bin/bash

count=0
while ((1)); do
    echo -ne "\rCount: $count"
    sleep 1;
    count=$(($count+1));
done     

If it doesn't make sense, just try it.

You could use that technique to indicate how many lines, or thousands of lines, have been processed so far. Tens or hundreds of lines may be good since it is not too often (more updates == slower runtime) but probably still often enough to show progress is continuing. You can specify a unit or just append zeros.

Note the use of -n and -e with echo, that is important.

You can also use \b (backspace) to similar effect.


In perl:

#!/usr/bin/perl
use strict;
use warnings FATAL => qw(all);

$| = 1;  # Pipeline stdout (i.e., no buffering).

my $count = 1;
while ($count) {
    print "\rCount $count";
    sleep 1;
    $count++;
}               
Related Question