如何在每个月的第一个工作日运行脚本?

时间:2010-09-27 05:09:37

标签: perl unix

我必须在每个月的第一个工作日运行一个脚本。请建议我如何在Perl中完成它。

假设如果在该国度假,该剧本应在第二个工作日运行。 我有一个二进制文件,如果它是特定国家的假期,它会给我前一个工作日的输出。

2 个答案:

答案 0 :(得分:7)

我建议您每天周一至周五使用cron运行脚本。

然后您的脚本将进行初始测试,如果测试失败则退出。

测试将是(伪代码):

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;

例如,isWeekend函数在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 );
}

您必须编写自己的public_holiday函数,以根据日期是否为您所在州/国家/地区的公共假期返回真值。

答案 1 :(得分:7)

CPAN包Date::Manip有各种好东西来支持这类事情。 'Date_NextWorkDay()'似乎最合适。

相关问题