Tomw72us has asked for the wisdom of the Perl Monks concerning the following question:

I am currently using a Korn shell script to launch Perl scripts in sequence. However the shell script doesn't give me much flexibility. I'm looking for a simple way in Perl to launch a series of perl scripts (1.pl, 2.pl, 3.pl for examples) and after each script is run, test to see if the script was able to run or not. If the script did not run, I need to generate an errorlog text file and put something in the text file (ie., Perl script 1.pl did not run). Thanks in advance! I am just learning Perl and it definitely has a learning curve! Note: I DO NOT want to use "warn" or a similar function to print to the screen. My errors should go to a text logfile. THANKS! Tom
  • Comment on Using Perl to launch another Perl script and generate error log if it fails

Replies are listed 'Best First'.
Re: Using Perl to launch another Perl script and generate error log if it fails
by Plankton (Vicar) on Mar 25, 2004 at 17:12 UTC
    Maybe you can have your script(s) work like this ...
    $ cat run.sh #!/bin/sh S=p1.pl && p1.pl && S=p2.pl && p2.pl && S=p3.pl && p3.pl if [ $? = 0 ] then echo "all completed ok" else echo "$S failed" fi $ cat p?.pl #!/usr/bin/perl #exit with no error exit 0; #!/usr/bin/perl # exit with error exit 1; #!/usr/bin/perl #exit with no error exit 0; $ ./run.sh p2.pl failed

    Plankton: 1% Evil, 99% Hot Gas.
Re: Using Perl to launch another Perl script and generate error log if it fails
by amw1 (Friar) on Mar 25, 2004 at 17:15 UTC
    Something like this should do it. This assumes that your perl scripts return 0 on success anything else for failure
    # untested @perlscripts = qw[1.pl 2.pl 3.pl]; foreach my $perlscript (@perlscripts) { system($perlscript); if($? == -1) { warn "Couldn't start $perlscript: $!"; } elsif($? != 0) { warn "Perl script $perlscript exited with: ". $? >> 8 } }
    replace the warns with any error handling youd want to do.
    Update: at the head of the file add
    open(<FH>, ">logfile.txt") || die "Doh: $!";
    and replace the warn's with
    print FH "warning message";
      If I wanted to replace the warn functions with a function that would write the same output to an error log, what would that look like? Thanks.

        If you aren't using your STDERR for anything else, just redirect it to the file and the warnings will show up there. You can do that in your shell script with foo.pl 2>> errlog.txt, or in Perl with:

        open STDERR, ">errlog.txt" or die "Whoa! I can't even log my errors: $!";

        Otherwise, you'll want the __WARN__ pseudo-signal:

        open ERRLOG, ">errlog.txt" or die; $SIG{__WARN__} = sub { my ($msg) = @_; $msg ||= q{Warning: something's wrong}; # Add the file and line number to $msg here # using the return values from caller(). print ERRLOG, $msg; };
        See also warn and caller in perlfunc and %SIG in perlvar.

        -- 
        LP^>

Re: Using Perl to launch another Perl script and generate error log if it fails
by kutsu (Priest) on Mar 25, 2004 at 21:58 UTC

    You could also put these scripts in modular form. tachyon has written a tutorial which should help explain this.

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce