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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.