0

I am trying to write a Perl CGI which executes an RKHunter scan. While executing the comman, I would like to show something to indicate progress instead of the actual output which is to be redirected to another file. The code thus far is:

open(my $quik_rk, '-|', 'rkhunter', '--enable', '"known_rkts"') or print "ERROR RUNNING QUICK ROOTKIT CHECK!!";
        while(<$quik_rk>)
        {       print ".";
        }       
        print "\n";
        close($quik_rk);

This doesn't show any output and I am presented with a blank screen while waiting for execution to complete. All the dots are printed to the screen together instead of one-by-one., Moreover, when I use the following to redirect, the command doesn't execute at all:

open(my $quik_rk, '-|', 'rkhunter', '--enable', '"known_rkts"', '>>', '/path/to/file') or print "ERROR RUNNING QUICK ROOTKIT CHECK!!";

How can I fix this in such a way that the verbose output is redirected to a file and only a .... steadily progresses on the screen?

rahuL
  • 3,330
  • 11
  • 54
  • 79

1 Answers1

2
$|=1;

At the beginning of your script. This turns autoflush on so every print actually prints instead of waiting for a newline before flushing the buffer.

Also see: http://perldoc.perl.org/perlvar.html#Variables-related-to-filehandles

DeVadder
  • 1,404
  • 10
  • 18
  • So do I need to turn off autoflush after the execution so that it resumes normal behaviour? – rahuL Dec 13 '13 at 09:34
  • It will continue to flush after every print command. So unless you actually want several print commands that have no newline to be buffered before actually beeing printed within the same script, you will probably not notice it beeing on. But you can of course turn it back off with `$|=0;` immediately afterwards. Then it will go back to only printing on encountering newlines. – DeVadder Dec 13 '13 at 09:39