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

         


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部