Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to run a process after every 10 minutes and in between i have to run another process. How do i go about it? I am trying the following psudo code

$pid=fork(); if($pid) { run one process; } while(1) { run the process after 10 minutes sleep 600; }

But I am not getting the desired result. Please help

Replies are listed 'Best First'.
Re: Concurrent process
by zentara (Cardinal) on Mar 29, 2012 at 11:57 UTC
    You might be better off using an eventloop. For instance, here is a Glib based loop, but many prefer AnyEvent
    #!/usr/bin/perl use warnings; use strict; use Glib; my $main_loop = Glib::MainLoop->new; my $timer1 = Glib::Timeout->add (1 , \&timer1 ); # 1ms delay #1000 milliseconds = 1 second my $timer2 = Glib::Timeout->add (600000 , \&timer2 ); # 600 sec delay sub timer1{ my $pid=fork(); if($pid){ run one process; { return 0; #return 1 to keep going, return 0 to stop timer } sub timer2{ my $pid=fork(); if($pid){ run one process; { return 1; #return 1 to keep going, return 0 to stop timer } $main_loop->run;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Concurrent process
by temporal (Pilgrim) on Mar 29, 2012 at 17:18 UTC

    zentara, not sure if your code addresses the problem. The OP seems to want to run one process continuously and every 10 minutes pause that process to run another.

    I might try something with alarm.

    #! /usr/bin/perl -w $SIG{ALRM} = \&ten_min_task; alarm(600); while (1) { # run in between task here #sleep 1; print "foo\n"; } sub ten_min_task { alarm(600); # run this code every 10 minutes #print "bar\n"; }

    Set the alarm timer back to 600s at the beginning of the ten minute task (as shown) to keep execution every 10 minutes regardless of its execution time. Put it at the end of the function if you want it to execute 10 minutes from its last completion.

      Just relying on alarm may not be the best choice, as things like sleep may interfere with it (OS Specific). Additionally, you may only have one alarm set at a time.

      It may, however, for the OP's problem, be a sufficient solution.

      --MidLifeXis