in reply to Using perl as a cron script?

If Schedule::Cron won't do what you're looking for (I suspect it probably will, but I've never used it), maybe you could use something like this:

#! /usr/bin/perl use strict; use warnings ; use diagnostics ; sub schedule (&@) { my ( $condition, $exec_stmt ) = @_ ; if ( &{$condition} ) { my $pid = fork() ; if ( !$pid ) { exec( $exec_stmt ) ; } } } while ( 1 ) { my ( $min, $hour, $mday, $mon, $year, $wday ) = ( localtime )[1..6 +] ; $year += 1900 ; # Run everyday at 12:00 schedule { $hour == 12 && $min == 00 } '/usr/bin/some/prog' ; # Run every three hours schedule { $hour % 3 == 0 } '/usr/sbin/some/other/prog' ; # Every Tuesday, plus the 1st & 15th of every month at midnight. schedule { ( $hour == 0 && $min == 0 ) && ( ( $wday == 2 ) || ( $mday == 1 || $mday == 15 ) ) } '/usr/local/bin/yet/another/prog' ; sleep 60 ; }

_______________
D a m n D i r t y A p e
Home Node | Email

Replies are listed 'Best First'.
Re^2: Using perl as a cron script?
by Anonymous Monk on Feb 05, 2021 at 06:29 UTC

    Thanks DamnDirtyApe, very useful script. I have a question, if the program sleep for 60 seconds, what prevents to run the same progran more than one time (since "hour%3 ==0" conditions is testing every 60 seconds ) ?

      This post is almost 20 years old and DamnDirtyApe hasn't been here for 12 years.

      But you are right, you need to check the minute too.

      And you should be aware that this is a hack, rounding errors could easily lead to skipped jobs.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery