Synopsis: I have a script (not CGI, *nix environment) that can 'die' for a variety of reasons. I want to restart the script automatically depending on what caused it to die.

Detail: It's a script that does many things including using Net::FTP to download some files over an extremely unreliable connenction. As a result, the Net::FTP session occasionally dies with an error like: "Timeout at /usr/local/lib/perl5/site_perl/5.6.0/Net/FTP.pm line 471" That's ok. I expect it to happen. It could also die for other reasons than a time out. Also note that the script uses lock files, and a bunch of other stuff that gets cleaned up in sig handler. e.g.

$SIG{__DIE__} = \&handle_die;
So, what I want to happen is to somehow have the script know if it died because Net::FTP timed out or for some other reason. If it was due to a time out, I want to restart the script. Possible solutions I've thought of for restarting it: use backticks or system() to execute the script from another script that restarts it, turn the existing script into a sub and call it from another script, etc. What I'd really like to do is have the signal handler in the script itself check this condition and restart itself. (i.e. a self resurrecting script) That avoids having an extra script to keep track of. Is this possible? Another problem is that I don't know how to tell what caused the script to 'die'. I looked at $? and it seems to be the same value no matter what caused the die. Is there a way to find out if Net::FTP caused it to die or if it was one of my own 'die' statements? I only want to restart for a time out condition, not any other error condition.

Any thoughts are welcome! Thanks,

--RT

PS -- yes, I know I can set the Timeout value in Net::FTP but that doesn't solve the problem. When the link goes down, it's down for a while.


The angel said to the women, "Do not be afraid, for I know that you are looking for Jesus, who was crucified. He is not here; he has risen, just as he said.

Matthew 28:5-6, NIV


In reply to Self resurrecting perl scripts by RhetTbull

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.