在Shell脚本中将Cron Job设置为每月的第一个工作日 [英] Set Cron Job for 1st working day of every month in Shell Scripting

查看:39
本文介绍了在Shell脚本中将Cron Job设置为每月的第一个工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是脚本语言的新手,有人可以解释一下如何在第一工作日设置cron作业吗?

I'm new to scripting language, can anyone please explain how to set the cron job for 1st working day?

推荐答案

首先,运行date命令:

First, run the date command:

$ date '+%x'
> 12/29/2014

%x告诉日期以当前语言环境的格式显示今天的日期.使用完全相同的格式,将假日列表放入名为假日的文件中.例如:

%x tells date to display today's date in the format of the current locale. Using that exact same format, put a list of holidays in a file called holidays. For example:

$ cat holidays
> 01/01/2015
> 07/04/2015

接下来,创建以下shell脚本:

Next, create the following shell script:

#!/bin/sh
dom=$(date '+%d') # 01-31, day of month
year=$(date '+%Y') # four-digit year
month=$(date '+%m') # two-digit month

nworkdays=0
for d in $(seq 1 $dom)
do
    today=$(date -d "$year-$month-$d" '+%x') # locale's date representation (e.g. 12/31/99)
    dow=$(date -d "$year-$month-$d" '+%u')   # day of week: 1-7 with 1=Monday, 7=Sunday
    if [ "$dow" -le 5 ]  && grep -vq "$today" /path/holidays
    then
        workday=Yes
        nworkdays=$((nworkdays+1))
    else
        workday=
    fi
done
[ "$workday" ] && [ "$nworkdays" -eq 1 ] && /path/command

这篇关于在Shell脚本中将Cron Job设置为每月的第一个工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆