in reply to Self resurrecting perl scripts

Actually, we have similar scripts to this. We have a threshold of so many errors before the script croaks. Basically, the script attempts to ftp to the file. If it fails, it calls a "redo" and keeps trying until there is success or the max number of failures is exceeded. Here are a couple of snippets from the code, to at least let you see how we do it.

sub get_files { # ftp files from remote server while (1) { $ftp = Net::FTP->new("$From_Host", Debug => 0); $ftp->login("$From_User","$From_Pass") || do { &print_ +ftp_error; redo; }; $ftp->cwd("$From_Dir"); (@dir_files = $ftp->dir) || do { &print_ftp_error; red +o; }; $ftp->quit; last; } sub print_ftp_error { # Error handling proc $save = $ftp->error; $ftp->quit; print STDERR "ftp error: $save\n"; print_to_log("ftp error: $save\n"); if (++$ftp_errors{"$save"} >= $Max_ftp_Errors) { print STDERR "\n\nExceeded maximum number of ftp error +s. Exiting!!!\n"; print_to_log("\n\nExceeded maximum number of ftp error +s. Exiting!!!\n"); system("$Notify_Proc \"ftpfiles.pl Exceeded maximum nu +mber of ftp errors. Please check $Log_File.\" 2>&1 > /dev/null"); rename "$Log_File", "$Log_File.$year.$mon.$mday\_$hour +.$min"; exit 1; } else { # # Give the ftp server a 60 second grace period to get # it's act together. # sleep 60; } }

birdbrane

Replies are listed 'Best First'.
Re: Re: Self resurrecting perl scripts
by RhetTbull (Curate) on Apr 12, 2001 at 18:41 UTC
    That's very similar to what I needed to do. Thanks for posting the snippet! I think I'll incorporate this idea into my script. I've got a "download_file" sub similar to your get_files so it should be a fairly easy fix. That seems much easier than trapping "die", etc. Although, I must admit that in working on this and reading the replies here I've learned *a lot* about perl that I didn't know two days ago! ;-)

    --RT