我在使用本机Unix CRON选项卡调度作业的应用程序上工作。参数说明如下:
$(function(){
var current = location.pathname;
$('#myTopnav a').each(function(){
var $this = $(this);
if($this.attr('href').indexOf(current) !== -1){
$this.addClass('active');
}
})
})
我想在一周的第一周的晚上8点开始工作,但我不知道如何确定一周的开始时间。是2017年12月31日 - 2018年1月6日第一周或第一周的1月7日或1月13日第一周?
答案 0 :(得分:5)
让cron作业在特定周数上运行并不容易 因为一切都取决于您的周数的定义 也用过。
这ISO 8601 standard在世界上被广泛使用:欧盟和其他大多数国家 欧洲国家,亚洲大部分地区和大洋洲
ISO 8601标准规定如下:
根据这个定义,可能有一个星期数53.这些发生在1月1日的a 星期五(例如,2016-01-01,2010-01-01)。或者,如果前一年是一个 闰年,也是星期六。 (例如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
并非所有国家/地区都使用ISO 8601系统。他们使用更绝对的方法。 美国系统用于加拿大,美国,新西兰,印度,日本...... 伊斯兰教系统通常用于中东地区。 两个系统非常相似。
美国:
伊斯兰:
根据这些定义,可能会有部分周 一年的开始和结束。因此,第一周和最后一周 年份不能包含所有工作日。
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
注意:这对您尝试的任务来说可能特别麻烦 执行。特别是如果它必须在第一个周一发生 周。本周一可能不存在。
将这些系统添加到cron无法直接进行。该 周测试应通过表格的条件测试来完成
weektestcmd weeknr && cmd
如果一个cronjob仅在系统时间20:00(如OP请求的话)的第4周的星期一运行,那么crontab将看起来像:
# 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
将weektestcmd
定义为
ISO 8601周数:
#!/usr/bin/env bash
[[ $(date '+%V') -eq $1 ]]
美国日历周数:
#!/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 ]]
伊斯兰日历周数:
#!/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 ]]
注意:请注意,在美国和伊斯兰教系统中,第1周可能没有星期一。
注意:还有其他定义周数的方法。尽管如此,方法保持不变。定义一个脚本,用于检查周数并在cron中使用它。
答案 1 :(得分:0)
您必须在crontab
中添加条件才能执行此操作。您的cron
会看起来
像这样的东西,
0 20 1-7 1 * root [ `date +%a` == "Mon" ] && /run/some/script
cron 0 20 1-7 1 *
每天晚上8点从1月1日到7日运行。
在执行脚本之前检查当天是星期一。
[ `date +%a` == "Mon" ]
有了这个,脚本将在7th January 2019
上运行,这是在一年的第一周内。
$ cal 01 2019
January 2019
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31