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

#!/usr/bin/perl use Date::Manip; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime t +ime; while { $time_val="$hour:$min:$sec"; if ($time_val = "01:01:00") { ./abc.sh PB } if ($time_val = "01:02:00") { ./abc.sh AB } }
How to wait at each step and check the time. and at the last move to the first step and wait until time is "01:01:00" and execute on next day. How to add a flag at each step when executed and move to the next step.

Replies are listed 'Best First'.
Re: Execute on conditional check
by almut (Canon) on Jun 20, 2010 at 13:32 UTC
    if ($time_val = "01:01:00") {

    I'm not really sure I understand what exactly you need to do... (something like cron?)  but replacing the assignment with a comparison will likely get you closer to success:

    if ($time_val eq "01:01:00") {

    Similarly, putting the ... = localtime time  in the loop, so the values to check against will actually change.

    Also, you probably meant  system "./abc.sh PB"; to run the shell script.

    Other than that, maybe add a sleep in the loop, so you don't consume unnecessarily large amounts of CPU power...

Re: Execute on conditional check
by CountZero (Bishop) on Jun 20, 2010 at 13:42 UTC
    if you are going to call some shell-script at certain moments every day, better to hand this task to cron.

    1 1 * * *  /your/first/script and 2 1 * * *  /your/second/script are crontab entries which will run these two scripts every day at 01:01:00 and 01:02:00</c> respectively.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Unfortunately, requirement is not to use it in a cron.:-( So need to do it in a script
      flag=0 time=$(date +"%H:%M:%S") echo $time while : do while : do if [ $(date +"%H:%M:%S") = '07:32:00' ] then echo "1st loop" break fi break done while : do if [ $(date +"%H:%M:%S") = '07:32:30' ] then echo "2nd loop" break fi break done done
      if condition is satisfied, it keeps printing out 1st loop. How to come out of the loop,if condition is satisfied.

        It seems to me that your shop does not allow cron for a reason. Could it be that they have a standard tool/package for scheduling of this type? Perhaps, something like UC4, TWS (Tivoli Workload Scheduler, AKA: Maestro), AutoSys, Control-M, CA-7, etc.

        These are very powerful tools; you might be better served looking into what your shop uses and working with the necessary team(s) to implement that way.

        This looks very much like a shell script. Shell scripts are not Perl. Please rewrite your shell script as a Perl script or ask your question elsewhere.

Re: Execute on conditional check
by ikegami (Patriarch) on Jun 20, 2010 at 21:31 UTC
    use strict; use warnings; use DateTime qw( ); use IPC::System::Simple qw( system ); my $now = DateTime->now( time_zone => 'locale' ); ( my $target = $now->clone() ) ->set_hour(1) ->set_minute(1) ->set_second(0); $target->add( days => 1 ) if $target < $now; for (;;) { my $dur = $target->epoch() - time(); sleep($dur) if $dur > 0; system( './abc.sh', 'PB' ); system( './abc.sh', 'AB' ); # XXX Assumes the tasks won't take more than a day. $target->add( days => 1 ); }

    IPC::System::Simple is used to add error checking to system. This is obviously good, but it's also a problem since it causes an exception that will prevent the task from being executed again. Tweak to your needs.