I have a Perl::Tk based script that does state mandated process monitoring and data collection for some industrial processes in the manufacturing plant where I work. (Serial interface to some National Instrument FieldPoint IO.) We need to do continuous monitoring with some fairly substantial fines if we exceed a threshold of downtime so I was concerned about this myself.

I ended up setting up the script to re exec itself once a day to avoid any side effects of memory leaks. You lose a second or two while it restarts but memory leaks can't get big enough to cause problems.

Here are the relevant bits. (Note: this is chopped out of a much larger script so some variables are artificially localized and/or not used in this fragment.)

use warnings; use strict; use Tk; use Date::Calc qw( Today_and_Now Add_Delta_DHMS ); use constant OS_Win => $^O =~ /Win/; my $last_time; my $time_zone = -5; # GMT-5 or, EST. Set TZ appropriately my $mw = MainWindow->new; my $repeat = $mw->repeat( 250, \&update ); MainLoop; sub update{ my ( $year, $month, $day, $hour, $minute, $second ) = Add_Delta_DHMS( Today_and_Now( [localtime] ), 0, $time_zone, 0, +0 ); # Restart the program every day at midnight. Sidestep a bunch of # memory leak problems. if ( ( "$hour$minute$second" eq '000' ) and ( $last_time eq '23595 +9' ) ) { OS_Win ? exec "wperl $0" : exec "perl $0 &"; } $last_time = "$hour$minute$second"; warn "$last_time\n"; # for testing purposes }

I'm not saying this is the only or best solution, but it works for me. The script this was taken from has been running "continuously" for the past several years.


In reply to Re: Long running Perl Tk processes by thundergnat
in thread Long running Perl Tk processes by greenFox

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.