本周在我的crontab工作中发生了一个问题 它设置如下,每两周一直正常工作直到现在。
10 06 * * 1 test $(($(date +\%W)\%2)) -eq 0 && echo 'test' > /tmp/test.log
问题是
$(($(日期+ \%W)\%2))将是08年2月7日。
如果你在bash中运行它会显示错误信息:value对于base来说太大了(错误令牌是“08”)。
当我尝试修改它以强制使用十进制基数时, 也无效 :
10 06 * * 1 test $((10#$(date +\%W)\%2)) -eq 0 && echo 'test' > /tmp/test.log
有人知道如何解决这个问题吗?非常感谢。
答案 0 :(得分:1)
每两周一次午夜
0 0 */15 * * echo 'test' > tmp.txt
Your cron job will be run at: (5 times displayed)
2016-03-15 00:00:00 UTC
2016-03-30 00:00:00 UTC
2016-04-15 00:00:00 UTC
2016-04-30 00:00:00 UTC
2016-05-15 00:00:00 UTC
答案 1 :(得分:1)
在crontab中不鼓励使用%
符号!
为了确保jod在 星期一 上开始,每两周,您最好创建一个小脚本。
#!/bin/bash
if [ "$1" == "-f" ] ;then # use "-f" switch to force execution on odd weeks
shift
else
printf -v d '%(%W)T' -1
((10#$d%2)) || exit 0 # stop here if odd week
fi
printf "Real job begin at %(%c)T, there...\n" -1
这可以缩短为一行:
#!/bin/bash
[ "$1" == "-f" ]&&shift||{ printf -v d '%(%W)T' -1;((10#$d%2))||exit;}
所以你可以在你的crontab中添加标准行:
10 06 * * 1 path/to/myScript > /tmp/test.log
date -d 2016-1-4\ 10:06 +%s
1451898360
for i in {0..52};do
printf -v d '%(%W)T' $((c=i*7*86400+1451898360))
((10#$d%2)) && printf "%(%a %d %b, week: %W)T\n" $c
done | cat -n
1 Mon 04 Jan, week: 01
2 Mon 18 Jan, week: 03
3 Mon 01 Feb, week: 05
4 Mon 15 Feb, week: 07
5 Mon 29 Feb, week: 09
6 Mon 14 Mar, week: 11
7 Mon 28 Mar, week: 13
8 Mon 11 Apr, week: 15
9 Mon 25 Apr, week: 17
10 Mon 09 May, week: 19
11 Mon 23 May, week: 21
12 Mon 06 Jun, week: 23
13 Mon 20 Jun, week: 25
14 Mon 04 Jul, week: 27
15 Mon 18 Jul, week: 29
16 Mon 01 Aug, week: 31
17 Mon 15 Aug, week: 33
18 Mon 29 Aug, week: 35
19 Mon 12 Sep, week: 37
20 Mon 26 Sep, week: 39
21 Mon 10 Oct, week: 41
22 Mon 24 Oct, week: 43
23 Mon 07 Nov, week: 45
24 Mon 21 Nov, week: 47
25 Mon 05 Dec, week: 49
26 Mon 19 Dec, week: 51
27 Mon 02 Jan, week: 01
对于每月使用 的想法进行比较:
for i in {1..12}/{15,30};do
date -d '2016/'$i +'%a %d %b, week: %W' 2>/dev/null
done | cat -n
1 Fri 15 Jan, week: 02
2 Sat 30 Jan, week: 04
3 Mon 15 Feb, week: 07
4 Tue 15 Mar, week: 11
5 Wed 30 Mar, week: 13
6 Fri 15 Apr, week: 15
7 Sat 30 Apr, week: 17
8 Sun 15 May, week: 19
9 Mon 30 May, week: 22
10 Wed 15 Jun, week: 24
11 Thu 30 Jun, week: 26
12 Fri 15 Jul, week: 28
13 Sat 30 Jul, week: 30
14 Mon 15 Aug, week: 33
15 Tue 30 Aug, week: 35
16 Thu 15 Sep, week: 37
17 Fri 30 Sep, week: 39
18 Sat 15 Oct, week: 41
19 Sun 30 Oct, week: 43
20 Tue 15 Nov, week: 46
21 Wed 30 Nov, week: 48
22 Thu 15 Dec, week: 50
23 Fri 30 Dec, week: 52
正如你所看到的那样,在几周内错过了一次手术:6,9,10,21,32和45.至少在一年内减少3次手术。