crontab 每月第二个周日

 man 5 crontab # 找到下述解释

Note: The day of a command's execution can be specified by two fields -- day of month, and day of week.   If  both fields  are  restricted (i.e., aren't *), the command will be run when either field matches the current time. 
For example, 
``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday. 
注: weekday 和 day 这两栏很容易造成混淆, 假如两栏同时都被指定时, 只需满足其中一栏就算符合,即会在每月1日和15日运行,每个星期五也会运行

解法:

1.指定日期,运行语句加上星期判断  

        如 每月第一个周日        

30 9 1-7 * * if [ $(date +"\%w") = "0" ]; then sh [脚本名] > /dev/null 2>&1;fi

         如 每月第二个周日        

30 9 8-14 * * if [ $(date +"\%w") = "0" ]; then sh [脚本名] > /dev/null 2>&1;fi

2.指定星期,运行语句加上日期判断 

         如 每月第二个周日        

30 9 * * 0 [ $(date +"\%d") -gt 7] && [ $(date +"\%d") -lt 15] && sh [脚本名] > /dev/null 2>&1

         如 每月第三个周二        

30 9 * * 2 [ $(date +"\%d") -gt 14] && [ $(date +"\%d") -lt 22] && sh [脚本名] > /dev/null 2>&1

3.指定星期,运行语句结合cal日历和awk获取指定位置日期判断

        如:每月第二个周六 指定周六,判断日期为日历中周六        

30 9 * * 6 [ $(date +"\%d") -eq $(cal | awk 'NR==3{print $NF}') ] && sh [脚本名] > /dev/null 2>&1

         

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值