I have a perl script that runs as a daemon looping all the time. I want to run a subfunction in a perl script that is based on the time (or timer) so every 2hrs it would run that subfunction and continue with it's loop. I'm thinking getting the epoch time and just checking it a few times through the loop and once it's greater then 2hrs it runs the subfunction. Is there a better way to do this in perl?
Thanks,
LF4
The word subroutines is used most in Perl programming because it is created using keyword sub. Whenever there is a call to the function, Perl stop executing all its program and jumps to the function to execute it and then returns back to the section of code that it was running earlier.
The most commonly used special variable is $_, which contains the default input and pattern-searching string. For example, in the following lines − #!/usr/bin/perl foreach ('hickory','dickory','doc') { print $_; print "\n"; }
This depends on whether there should be 2 hours since the START of the last subroutine launch, or since the END of last execution.
1) If the latter (2 hours between the end of running the last subroutine and the start of new one), cespinoza's solution is perfectly acceptable (loop infinitely, and call sleep(7200);
after executing the subroutine).
my $timeout = 7200;
while (1) {
dostuff();
sleep($timeout);
};
The only problem with this is that it can't handle the case where dostuff()
takes forever, e.g. gets stuck - for the discussion of why it's an important situation to consider and approaches to solve, see below.
2) If the former (2 hours between starting points), you have three options, related to handling the subroutine run-time that exceeds 2 hours[0]. Your 3 options, explained in detail below, are to either:
2a) kick off a new subroutine while the old one keeps running (in parallel);
2b) to kick off a new subroutine AFTER the old one finishes;
2c) to kick off a new subroutine but first stop the execution of the prior one.
2a an 2c options require you to set an alarm()
for 2 hours, and differ in what happens when an alarm gets triggered.
[0] NOTE: since any subroutine is likely to require at least SOME resources from the PC, there's always a - however small - chance that it would exceed 2 hours, so you have to pick one of those 3 options to handle such a scenario.
2a) Kick off every 2 hours, running in parallel with old execution if not finished.
This option is, essentially, implementing cron
functionality.
Anytime you hear the word parallel, you would likely fork off the process.
my $timeout = 7200;
while (1) { # Not tested!
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
if (!defined($child_pid = fork())) {
die "cannot fork: $!\n";
} elsif (!$child_pid) { # Child
dostuff();
exit;
} # Parent continues to sleep for 2 hours
alarm $timeout; # You need it in case forking off take >2hrs
sleep; # forever
};
die unless $@ eq "alarm\n"; # propagate unexpected errors
# We don't need to check if $@ is true due to forever sleep
}
2b) Kick off every 2 hours, if the old one didn't finish, let it run till it finishes
This can be re-worded as "kick off task, if it finishes faster than 2 hours, sleep for the remainder"
my $timeout = 7200;
while (1) {
my $start = time;
dostuff();
my $end = time;
my $lasted = $end - $start;
if ($lasted < $timeout) {
sleep($timeout - $lasted);
}
};
2c) Kick off every two hours, if the previous one didn't finish, time it out and kill it
Whenever you see logic like this, alarm is obviously the answer.
while (1) {
my $finished = 0;
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 7200;
dostuff();
$finished = 1;
sleep; # forever
};
die unless $@ eq "alarm\n"; # propagate unexpected errors
warn "Timed out!!!\n" unless $finished
}
P.S. As cespinoza noted, you need to somehow daemonize the script (ensure it doesn't get killed when you exit the shell that started it), by either Unix means (e.g. launching it as nohup) or Perlish means (search for daemonize + Perl on Stackoverflow for mechanics of that).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With