script hangs when run from command line and redirecting stdout and stderr to file

Discussion in 'Perl Misc' started by it_says_BALLS_on_your forehead, Jan 5, 2006.

  1. when i run this perl script from the command line directly, there is no
    problem. when i run it like so:

    script.pl > script.log 2>&1 &


    ....i noticed that script.log ceased being written to after a while--and
    not always at the same point.

    my script has print statements in it, which should be redirected to
    script.log. i tried running the above line a couple of times, and some
    of the lines that i printed were cut off in mid-string. i received no
    error message though, and when i did a ps -ef | grep script.pl the
    process was still running.

    one pattern i noticed was that it always stopped after the first
    &measure_url_performance was done. this may be because i only ran it
    twice this way.

    here is a short but (hopefully) complete working script:

    #!/usr/bin/perl
    use strict; use warnings;
    use File::Find;
    use Date::Manip;
    use Mail::Sender;
    use Statistics::Descriptive;
    require "tools.pl";


    my (
    $beginDate,
    $numDays,
    $chkCount,
    $printDate,
    $allRecs,
    $logDate, # Log Date to look for in the format "YYDDMM", example
    030509 for 5/9/03
    $avgDuration,
    $mailBody,
    $maxDuration,
    $minDuration,
    $noDuration,
    $pctile90,
    $parseString, # String to look for in the logs. Enclose in
    quotes if spaces are included.
    $totFiles, # Total number of files reviewed.
    $recCount, # Total records found in all files that match the
    parseString
    $stat, # Statistics::Descriptive object
    $yesterday
    );

    my @one_dirs = glob("/path1");
    my @one_urls = ('this/is/a/string');

    my @two_dirs = glob("/path2");
    my @two_urls = ('this/is/another/string');

    measure_url_performance( \@one_urls, \@one_dirs, 1 );
    measure_url_performance( \@two_urls, \@two_dirs, 2 );

    sub measure_url_performance {
    my ($urls_ref, $dirs_ref, $type) = @_;

    my @urls = $urls_ref ? @{$urls_ref} : ();
    my @dirs = $dirs_ref ? @{$dirs_ref} : ();

    my $wanted = get_parse_routine($type);

    foreach (@urls) {
    $parseString = $_;
    print "parseString is -$parseString-\n";
    $stat = Statistics::Descriptive::Full->new();
    for my $diff ( 0..$numDays ) {
    $logDate = UnixDate(DateCalc($beginDate, " + $diff day"),
    '%y%m%d');
    $printDate = $logDate;
    $printDate =~ s/(\d\d)(\d\d)(\d\d)/$2\/$3\/$1/;
    $recCount = 0;
    print "logDate is -$logDate-\n";
    # find( \&process_file, @dirs );
    find( $wanted, @dirs );
    $allRecs += $recCount;
    }
    $pctile90 = sprintf( "%.3f", $stat->percentile(90) );
    $avgDuration = sprintf( "%.3f",$stat->mean() );
    $minDuration = sprintf( "%.3f", $stat->min() );
    $maxDuration = sprintf( "%.3f", $stat->max() );
    print LOGINCSV
    "$parseString,$beginDate,$printDate,$allRecs,$pctile90,$avgDuration,$minDuration,$maxDuration\n";
    print "Found $allRecs records with string $parseString in a
    total of $totFiles files, $noDuration with no duration.\n";
    $allRecs = $totFiles = 0;
    }

    }

    sub get_parse_routine {
    my ( $type ) = @_;
    return \&parse_ewp if ( $type == 1 );
    return \&parse_pslogin if ( $type == 2 );
    return \&parse_login if ( $type == 3 );
    }

    sub parse_ewp {
    my $fileName = $File::Find::name;
    return unless ($fileName =~ /^.*$logDate\.gz$/);

    print "Ready to process -$fileName-\n";
    print "test scope, parseString set in calling context
    -$parseString-\n";

    my $chkCount;

    $totFiles++;
    open (ZIPFILE, "gzip -dc $fileName |") || die "Can't open $fileName
    $!\n";
    while (<ZIPFILE>) {
    next unless /\Q$parseString\E/i;
    $chkCount++;
    chomp;

    my $part2 = (split /"/,$_)[2];
    my $duration = (split / /,$part2)[3];

    # print "part2 is -$part2- duration is -$duration-\n";

    if ($duration =~ /^\d[\d\.]*$/) {
    $stat->add_data($duration);
    $recCount++;
    }
    else {
    $noDuration++;
    }
    next;
    if ($chkCount > 10) {
    $chkCount = 0;
    last;
    }
    }
    close ZIPFILE;
    }

    sub parse_pslogin {
    my $fileName = $File::Find::name;
    return unless ($fileName =~ /^.*$logDate\.gz$/);

    print "Ready to process -$fileName-\n";
    print "test scope, parseString set in calling context
    -$parseString-\n";

    my $chkCount;

    $totFiles++;
    open ( ZIPFILE, "gzip -dc $fileName |" ) || die "Can't open
    $fileName $!\n";
    while ( <ZIPFILE> ) {
    next unless /\Q$parseString\E/i;
    $chkCount++;
    chomp;

    my $part6 = (split /"/,$_)[6];
    my $duration = (split /\ /,$part6)[5];

    # print "part6 is -$part6- duration is -$duration-\n";

    if ( $duration =~ /^\d[\d\.]*$/ ) {
    $stat->add_data($duration);
    $recCount++;
    }
    else {
    $noDuration++;
    }
    next;
    if ($chkCount > 10) {
    $chkCount = 0;
    last;
    }
    my $part2 = (split /"/,$_)[2];
    my $duration = (split / /,$part2)[3];

    # print "part2 is -$part2- duration is -$duration-\n";

    if ($duration =~ /^\d[\d\.]*$/) {
    $stat->add_data($duration);
    $recCount++;
    }
    else {
    $noDuration++;
    }
    next;
    if ($chkCount > 10) {
    $chkCount = 0;
    last;
    }
    }
    close ZIPFILE;
    }



    sub parse_login {
    my $fileName = $File::Find::name;
    return unless ($fileName =~ /^.*$logDate\.gz$/);

    print "Ready to process -$fileName-\n";
    print "test scope, parseString set in calling context
    -$parseString-\n";

    my $chkCount;

    $totFiles++;
    open ( ZIPFILE, "gzip -dc $fileName |" ) || die "Can't open
    $fileName $!\n";
    while ( <ZIPFILE> ) {
    next unless /\Q$parseString\E/i;
    $chkCount++;
    chomp;

    my $duration = (split /\t/,$_)[13];

    # print "duration is -$duration-\n";

    if ( $duration =~ /^\d[\d\.]*$/ ) {
    $stat->add_data($duration);
    $recCount++;
    }
    else {
    $noDuration++;
    }
    next;
    if ($chkCount > 10) {
    $chkCount = 0;
    last;
    }
    }
    close ZIPFILE;
    }
    it_says_BALLS_on_your forehead, Jan 5, 2006
    #1
    1. Advertising

  2. it_says_BALLS_on_your forehead

    Anno Siegel Guest

    it_says_BALLS_on_your forehead <> wrote in comp.lang.perl.misc:
    > when i run this perl script from the command line directly, there is no
    > problem. when i run it like so:
    >
    > script.pl > script.log 2>&1 &
    >
    >
    > ...i noticed that script.log ceased being written to after a while--and
    > not always at the same point.
    >
    > my script has print statements in it, which should be redirected to
    > script.log. i tried running the above line a couple of times, and some
    > of the lines that i printed were cut off in mid-string. i received no
    > error message though, and when i did a ps -ef | grep script.pl the
    > process was still running.
    >
    > one pattern i noticed was that it always stopped after the first
    > &measure_url_performance was done. this may be because i only ran it
    > twice this way.
    >
    > here is a short but (hopefully) complete working script:


    Hopefully? Didn't you make a test run?

    > #!/usr/bin/perl
    > use strict; use warnings;
    > use File::Find;
    > use Date::Manip;
    > use Mail::Sender;


    Where are you using Mail::Sender? You are making it harder than
    to run your script.

    > use Statistics::Descriptive;
    > require "tools.pl";


    Where is tools.pl, what is in it. Do you need it?

    > my (
    > $beginDate,
    > $numDays,
    > $chkCount,
    > $printDate,
    > $allRecs,
    > $logDate, # Log Date to look for in the format "YYDDMM", example
    > 030509 for 5/9/03
    > $avgDuration,
    > $mailBody,
    > $maxDuration,
    > $minDuration,
    > $noDuration,
    > $pctile90,
    > $parseString, # String to look for in the logs. Enclose in
    > quotes if spaces are included.
    > $totFiles, # Total number of files reviewed.
    > $recCount, # Total records found in all files that match the
    > parseString
    > $stat, # Statistics::Descriptive object
    > $yesterday
    > );


    Don't declare variables in one bulk. Declare each one close to its
    first usage. Also, your comments wrap. Keep the line length below 72
    characters on Usenet, in code as well as in other text.

    > my @one_dirs = glob("/path1");
    > my @one_urls = ('this/is/a/string');
    >
    > my @two_dirs = glob("/path2");
    > my @two_urls = ('this/is/another/string');
    >
    > measure_url_performance( \@one_urls, \@one_dirs, 1 );
    > measure_url_performance( \@two_urls, \@two_dirs, 2 );
    >
    > sub measure_url_performance {
    > my ($urls_ref, $dirs_ref, $type) = @_;
    >
    > my @urls = $urls_ref ? @{$urls_ref} : ();
    > my @dirs = $dirs_ref ? @{$dirs_ref} : ();
    >
    > my $wanted = get_parse_routine($type);
    >
    > foreach (@urls) {
    > $parseString = $_;
    > print "parseString is -$parseString-\n";
    > $stat = Statistics::Descriptive::Full->new();
    > for my $diff ( 0..$numDays ) {


    $numDays is a global variable (why?) that is nowhere set. Giving up
    at this point. Too many errors, too much cruft. Post a minimal runnable
    program.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Jan 6, 2006
    #2
    1. Advertising

  3. it_says_BALLS_on_your forehead

    Joe Smith Guest

    Re: script hangs when run from command line and redirecting stdoutand stderr to file

    it_says_BALLS_on_your forehead wrote:

    > my script has print statements in it, which should be redirected to
    > script.log. i tried running the above line a couple of times, and some
    > of the lines that i printed were cut off in mid-string.


    That's what happens when you neglect to set $| to a nonzero value.
    Just add
    $|++;
    near the start of your program.
    -Joe
    Joe Smith, Jan 10, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Landenburer

    redirecting stderr and stdout

    Jon Landenburer, May 12, 2004, in forum: Perl
    Replies:
    1
    Views:
    11,192
    Joe Smith
    May 13, 2004
  2. Jan Knop
    Replies:
    4
    Views:
    1,003
    Jan knob
    Nov 24, 2003
  3. Michael McGarry
    Replies:
    1
    Views:
    593
    Michael Fuhr
    Dec 16, 2004
  4. Lincoln Yeoh

    Redirecting stderr and stdout to syslog

    Lincoln Yeoh, Aug 5, 2008, in forum: Python
    Replies:
    2
    Views:
    2,101
    Lincoln Yeoh
    Aug 6, 2008
  5. jonathan
    Replies:
    6
    Views:
    147
    Jay Tilton
    Nov 25, 2003
Loading...

Share This Page