Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

printing during process

by Anonymous Monk
on Apr 05, 2012 at 05:45 UTC ( #963585=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

how do I get a script to print as it is working instead of when it is done?

I am running it on a shell command but all the output comes out at the same time, how do I get it to print as it comes across a print statement instead of waiting until the whole script is done running?


Replies are listed 'Best First'.
Re: printing during process
by stevieb (Abbot) on Apr 05, 2012 at 06:03 UTC

    It's hard to tell exactly what you mean when you refer to "shell command", particularly because you don't show us anything you've tried, but here's a simple Perl snippet that shows how to do what you want.

    #!/usr/bin/perl use strict; use warnings; open my $fh, '<', 'file.txt' or die "Can't open file: $!"; while ( my $line = <$fh> ){ chomp $line; # do stuff print "$line\n"; }
      I am running a script to scan my website for vulnerabilities, when it finds them I want it to print the page, then continue, so it is running a while statement.

      I don't run it in a browser, I run it in shell:
      perl scanSite.cgi shell=1 dir=pathToStart
      So the script runs for 1 hundred thousand scans, always looking for vulnerabilities and sleeping 1 second in between, I have it print a period every 1 minute so that I know it is still running and when it finds something that matches what I have it looking for on the page, it prints the url and what it found.

      The problem is the print commands do not print anything, no periods or anything, but if I put a exit command in then it prints everything all at one time.

      What I want it to do is print stuff as it comes across the print command and not wait until the script is done with everything and comes across the exit command.

      Is there a perl command to have it print the output as it comes across it and not wait until the script is done?


        The only way anyone will know how to help you is if you post the relevant code from the CGI file you're using. However, the first thing that comes to mind is autoflush:

        local $| = 1;
        "the script runs for 1 hundred thousand scans, always looking for vulnerabilities and sleeping 1 second in between"

        So the sleep process, alone ((100_000/60)/60), eats up nearly 28 hours? Is it even sane to spread your output over more than a day?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://963585]
Approved by Old_Gray_Bear
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2018-02-25 23:58 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (315 votes). Check out past polls.