如何安排Cron作业在一年的第4周运行 [英] How to schedule a Cron job to run 4th week of the year

查看:89
本文介绍了如何安排Cron作业在一年的第4周运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用本机Unix CRON选项卡安排作业的应用程序上工作。参数的说明如下:

 分钟,小时,Da_of_Week(1-7,1 = Sun),Day_of_Month(1 -31),Day_of_Year(1-365),week(1-52),month(1-12)

我想在每年第一周的晚上8点开始工作,但是我不知道如何确定一周的开始时间。是第一周是2017年12月31日至2018年1月6日,还是第一周是从2018年1月7日至1月13日?在特定的星期数上运行
并不容易,因为一切都取决于您也是
所使用的星期数的定义。



欧洲(ISO 8601)



ISO 8601标准是在世界范围内广泛使用:欧盟和其他大多数
欧洲国家,大部分亚洲国家和大洋洲



ISO 8601标准规定:




  • 一周有7天

  • 一周的第一天是星期一

  • 第一周是一年中的第一周,其中包含
    星期四。这意味着它是一月份的第一周,其中
    超过4天。



使用此定义,可以有一个星期数字53。这些发生在1月1日是
星期五(例如2016年1月1日,2010年1月1日)。或者,如果前一年是leapb $ b的leap年,则也是星期六。 (例如2005-01-01)

  2015年12月2016年1月
Mo Tu We Th Fr Sa Su CW Mo Tu我们Sa Su Cr
1 2 3 4 5 6 49 1 2 3 53
7 8 9 10 11 12 13 50 4 5 6 7 8 9 10 01
14 15 16 17 18 19 20 51 11 12 13 14 15 16 17 02
21 22 23 24 25 26 27 52 18 19 20 21 22 23 24 03
28 29 30 31 53 25 26 27 28 29 30 31 04



美国或伊斯兰教(非ISO 8601)



并非所有国家都使用ISO 8601系统。他们使用更绝对的方法。
美国系统在加拿大,美国,新西兰,印度,日本,...
伊斯兰系统通常在中东使用。
这两个系统非常相似。



美国人:




  • 一周有7天

  • 一周的第一天是星期日

  • 第一周从1月1日开始



伊斯兰语:




  • 一周有7天

  • 一周的第一天是星期六

  • 第一周从1月1日开始



有了这些定义,就有可能
在一年中的开始和结束的部分时间。因此,
年的第一周和最后一周不能包含所有工作日。

 美国人:

2015年12月2016年1月
Su Mo Tu We Th Fr Sa CW Su Mo Tu We Th Fr Sa CW
1 2 3 4 5 49 1 2 01
6 7 8 9 10 11 12 50 3 4 5 6 7 8 9 02
13 14 15 16 17 18 19 51 10 11 12 13 14 15 16 03
20 21 22 23 24 25 26 52 17 18 19 20 21 22 23 04
27 28 29 30 31 53 24 25 26 27 28 29 30 05
31 06

伊斯兰教徒:

2015年12月2016年1月
Sa Su Mo Tu We Th Fr CW Sa Su Mo Tu We Fr CW
1 2 3 4 49 1 01
5 6 7 8 9 10 11 50 2 3 4 5 6 7 8 02
12 13 14 15 16 17 18 51 9 10 11 12 13 14 15 03
19 20 21 22 23 24 25 52 16 17 18 19 20 21 22 04
26 27 28 29 30 31 53 23 2425 26 27 28 29 05
30 31 06

注意:对于您尝试
执行的任务,这可能特别麻烦。特别是如果它必须发生在第一个
周的星期一。这个星期一可能不存在。



将其导入cron



无法将这些系统添加到cron中直接地
周测试应通过以下形式的条件测试来完成:

  weektestcmd weeknr&& cmd 

对于cronjob仅在每年第4周的星期一20点运行: 00系统时间(根据OP的要求),那么crontab将如下所示:

 #作业定义示例:
#.----------------分钟(0-59)
#| .-------------小时(0-23)
#| | .----------每月的某天(1-31)
#| | | .------- month(1-12)OR jan,feb,mar,apr ...
#| | | | .----星期几(0-6)(星期日= 0或7)
#| | | | |
#* * * * *要执行的命令
0 20 * * 1 weektestcmd 4&& cmd

weektestcmd 定义为



ISO 8601周编号:

 #! / usr / bin / env bash 
[[$(date'+%V')-eq $ 1]]

美国日历周编号:

 #!/ usr / bin / env bash 
#获取一年中的日期
doy = $(date +%j)
#计算一月一日的周偏移量
##计算日期Mo = 1 .. Su = 7
offset = $(date -d $(date +%Y)-01-01 +%u)
##取模对于Su = 0的偏移量
offset = $(((offset%7))
#计算当前星期数
cw = $(((doy + offset + 6)/ 7) )
[[$ cw -eq $ 1]]

伊斯兰日历周数:

 #!/ usr / bin / env bash 
#获取一年中的日期
doy = $(date +%j)
#计算1月1日的星期偏移量
##计算星期三k且Mo = 1 .. Su = 7
offset = $(date -d $(date +%Y)-01-01 +%u)
##取模为偏移量为Sa = 0
offset = $((((offset + 1)%7))
#计算当前周数
cw = $(((doy + offset + 6) / 7))
[[$ cw -eq $ 1]]

注意:请注意,在美国和伊斯兰教系统中,第1周可能没有星期一。



注意:还有其他定义周数的方法。但是,方法保持不变。定义一个检查周数并在cron中使用的脚本。


I work on an application that uses native Unix CRON tab for scheduling jobs. the description of parameters are as follows :

Minute, Hour, Da_of_Week(1-7, 1=Sun), Day_of_Month(1-31), Day_of_Year(1-365), Week (1-52), Month (1-12) 

I want to run a job on Monday of the 1st week of the year at 8 pm, but I don't know how to determine when the week starts. Is 31 Dec 2017 - 06th Jan 2018 a first week or 7th Jan to 13th Jan 2018 a first week ?

解决方案

Having cron jobs running on particular week numbers is not easy as everything depends on the definition of week numbers you are used too.

European (ISO 8601)

This ISO 8601 standard is widely used in the world: EU and most of other European countries, most of Asia, and Oceania

The ISO 8601 standard states the following:

  • There are 7 days in a week
  • The first day of the week is a Monday
  • The first week is the first week of the year which contains a Thursday. This means it is the first week with 4 days or more in January.

With this definition, it is possible to have a week number 53. These occur with the first of January is on a Friday (E.g. 2016-01-01, 2010-01-01). Or, if the year before was a leap year, also a Saturday. (E.g. 2005-01-01)

   December 2015               January 2016        
 Mo Tu We Th Fr Sa Su CW    Mo Tu We Th Fr Sa Su CW
     1  2  3  4  5  6 49                 1  2  3 53
  7  8  9 10 11 12 13 50     4  5  6  7  8  9 10 01
 14 15 16 17 18 19 20 51    11 12 13 14 15 16 17 02
 21 22 23 24 25 26 27 52    18 19 20 21 22 23 24 03
 28 29 30 31          53    25 26 27 28 29 30 31 04

American or Islamic (Not ISO 8601)

Not all countries use the ISO 8601 system. They use a more absolute approach. The American system is used in Canada, United States, New Zealand, India, Japan,... The Islamic system is generally used in the middle east. Both systems are very similar.

American:

  • There are 7 days in a week
  • The first day of the week is a Sunday
  • The first week starts on the 1st of January

Islamic:

  • There are 7 days in a week
  • The first day of the week is a Saturday
  • The first week starts on the 1st of January

With these definitions, it is possible to have partial weeks at the beginning and the end of a year. Hence the first and last week of the year could not contain all weekdays.

American:

    December 2015                January 2016       
 Su Mo Tu We Th Fr Sa CW     Su Mo Tu We Th Fr Sa CW
        1  2  3  4  5 49                     1  2 01
  6  7  8  9 10 11 12 50      3  4  5  6  7  8  9 02
 13 14 15 16 17 18 19 51     10 11 12 13 14 15 16 03
 20 21 22 23 24 25 26 52     17 18 19 20 21 22 23 04
 27 28 29 30 31       53     24 25 26 27 28 29 30 05
                             31                   06

Islamic:

   December 2015                 January 2016       
 Sa Su Mo Tu We Th Fr CW     Sa Su Mo Tu We Th Fr CW
           1  2  3  4 49                        1 01
  5  6  7  8  9 10 11 50      2  3  4  5  6  7  8 02
 12 13 14 15 16 17 18 51      9 10 11 12 13 14 15 03
 19 20 21 22 23 24 25 52     16 17 18 19 20 21 22 04
 26 27 28 29 30 31    53     23 24 25 26 27 28 29 05
                             30 31                06

Note: this could be particularly cumbersome for the task you try to perform. Especially if it has to occur on the Monday of the first week. This Monday might not exist.

Importing this in the cron

Adding these systems to the cron cannot be done in a direct way. The week testing should be done by means of a conditional test of the form

weektestcmd weeknr && cmd

For a cronjob to be run only on the Monday of the 4th week of the year at 20:00 system time (as the OP requested), the crontab would look then as:

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  *   command to be executed
  0 20  *  *  1   weektestcmd 4 && cmd

With weektestcmd defined as

ISO 8601 week numbers:

#!/usr/bin/env bash
[[ $(date '+%V') -eq $1 ]]

American calendar week numbers:

#!/usr/bin/env bash
# obtain the day of year
doy=$(date "+%j")
# compute the week offset of the first of January
## compute the day of the week with Mo=1 .. Su=7
offset=$(date -d $(date "+%Y")-01-01 "+%u")
## Take the modulo for the offset as Su=0
offset=$(( offset%7 ))
# Compute the current week number
cw=$(( (doy + offset + 6)/7 ))
[[ $cw -eq $1 ]]

Islamic calendar week numbers:

#!/usr/bin/env bash
# obtain the day of year
doy=$(date "+%j")
# compute the week offset of the first of January
## compute the day of the week with Mo=1 .. Su=7
offset=$(date -d $(date "+%Y")-01-01 "+%u")
## Take the modulo for the offset as Sa=0
offset=$(( (offset + 1)%7 ))
# Compute the current week number
cw=$(( (doy + offset + 6)/7 ))
[[ $cw -eq $1 ]]

Note: Be aware that in the American and Islamic system it might be possible not to have a Monday in week 1.

Note: There are other methods of defining a week number. Nonetheless, the approach stays the same. Define a script which checks the week number and use it in the cron.

这篇关于如何安排Cron作业在一年的第4周运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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