Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
P is for Practical
 
PerlMonks  

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?

Thanks,
Richard

Comment on printing during process
Re: printing during process
by stevieb (Hermit) 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?

      Thanks,
      Richard

        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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://963585]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-04-17 04:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (439 votes), past polls