in reply to Long running Perl Tk processes

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.