Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6370541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:56:36+00:00 2026-05-25T00:56:36+00:00

I have a perl script that runs as a daemon looping all the time.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:56:37+00:00Added an answer on May 25, 2026 at 12:56 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl script that I'd like to run on Windows, using either
Currently I have a perl script that runs forever on my server, checking a
I have written a Perl script that runs as a daily crontab job that
I currently have a Perl script that runs an external command on the system,
Started to learn some shell scripting. I have a perl script that runs on
I have a simple perl script that uses DBD::Oracle to run a query and
I have a perl script that runs a series of batch scripts for regression
I have a Perl script that runs a different utility (called Radmind , for
I have a perl script that I need to tweak. The script runs and
I have a Perl script that I'm attempting to set up using Perl Threads

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.