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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With