Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a script on the 1st working day of every month?

Tags:

unix

perl

I have to run a script on the 1st working day of every month. Please suggest how I can do it in Perl.

Lets say if its national holiday in that country, the script should run on 2nd working day. I have one binary which give me output of previous working day if its holiday for specific country.

like image 425
user363638 Avatar asked Dec 09 '22 13:12

user363638


1 Answers

I suggest you run your script using cron once every day Mon-Fri.

Then your script would have an initial test, and exit if the test fails.

The test would be (pseudo code):

if ( isWeekend( today ) ) {
    exit;
} elsif ( public_holiday( today ) ) {
    exit;
}
for ( day_of_month = 1; day_of_month < today; day_of_month++ ) {
    next if ( isWeekend( day_of_month ) );

    if ( ! public_holiday( day_of_month ) ) {
        # a valid day earlier in the month wasn't a public holiday
        # thus this script MUST have successfully run, so exit
        exit;
    }
}

# run script, because today is NOT the weekend, NOT a public holiday, and
#   no possible valid days for running exist earlier in this month
1;

For example, an isWeekend function might look like this in Perl:

sub isWeekend {
    my ( $epoch_time ) = @_;

    my $day_of_week = ( localtime( $epoch_time ) )[6];
    return( 1 ) if ( $day_of_week == 0 ); # Sunday
    return( 1 ) if ( $day_of_week == 6 ); # Saturday
    return( 0 );
}

You would have to write your own public_holiday function to return a truth value depending on whether a date was a public holiday in your specific state/country.

like image 54
PP. Avatar answered May 29 '23 10:05

PP.