Transfer the file, but do not overwrite it. When your program notices that a new version has arrived, it writes its current state out to harddisk and runs the updater, which just switches the files around.
#!/usr/bin/perl # 529755.pl use strict; use diagnostics; my $importantdatatomangle = 1; sub checkfornew { my $newversionfilename = '529755.pl.newversion'; if ( -e $newversionfilename # does the new file exist? and (time - (stat $newversionfilename)[9] > 2) # is it older than 2 sec, i.e. has it been completely # transferred to this here box? ) { # save the current state of the program out to harddisk # i.e. $importantdatatomangle # ... warn "found a new version, running updater"; exec 'perl updater.pl' or die "could not exec updater: $!"; # run the updater # see perldoc -f exec why I included error checking }; }; # main program starts below here while (1) { # loop for "many hours", actually forever :p { # do important work $importantdatatomangle = -$importantdatatomangle; warn "data is now $importantdatatomangle"; sleep 5; warn "zzzzz"; }; checkfornew; # periodically check for newer version };
Actually you have to read the current state from harddisk back into your variables at the very beginning of the program. I'm just setting my variable to 1 out of laziness.
#!/usr/bin/perl # updater.pl use strict; use diagnostics; use File::Copy; # improve this shoddy programming, # remember to actually check the return values of move for errors move '529755.pl', '529755.pl~'; # create backup move '529755.pl.newversion', '529755.pl'; # deploy new version warn "deployed new version, going to run it now"; exec 'perl 529755.pl' or die "could not exec new version: $!";
Next time on perlmonks, show what you have programmed so far, or else you get slapped with a wet octopus.

In reply to Re: Restart Long Running Perl Script by Anonymous Monk
in thread Restart Long Running Perl Script by avo

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.