我也有类似的情况。我的目标是为非Unix极客提供一个非常简单的解决方案。
当我的Linux服务器重新启动时,它运行一个发送警报的脚本。但是,如果这是在“维护窗口”(定义为星期六晚上11点至星期日早上6点)之间发生的,则我不希望发送该页面,因为在此窗口中重新启动是可以的(已应用补丁等)。我已经对其进行了测试,并且确实按预期工作(据我所知)。
这是参考日历:
June 2018
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
这是我的测试代码:
FMT='+%k:%a' # i.e. Sat:23 for Saturday at 11pm local time
TESTDATE1=$(date -d '2018-06-23 14:30:00' $FMT)
TESTDATE2=$(date -d '2018-06-23 23:59:59' $FMT)
TESTDATE3=$(date -d '2018-06-24 00:00:00' $FMT)
TESTDATE4=$(date -d '2018-06-24 05:59:59' $FMT)
TESTDATE5=$(date -d '2018-06-24 08:01:00' $FMT)
for VAL in "${TESTDATE1}" $TESTDATE2 $TESTDATE3 $TESTDATE4 $TESTDATE5
do
#extract the 24 hour value and the day
HH="$(echo ${VAL} | cut -d':' -f1)"
DAY="$(echo ${VAL} | cut -d':' -f2)"
echo Testing $VAL $HH $DAY
if [[ "$DAY" = "Sat" && $HH -ge 23 ]]; then
echo ' Shhhh People are trying to sleep ... Sat window'
elif [[ "$DAY" = "Sun" && $HH -le 6 ]]; then
echo ' People are trying to sleep ... Sun window'
else
echo ' PAGE SOMEONE - time is outside the Maintenance Window'
fi
done
这是输出
Testing 14:Sat 14 Sat
PAGE SOMEONE - time is outside the Maintenance Window
Testing 23:Sat 23 Sat
Shhhh People are trying to sleep ... Sat window
Testing 0:Sun 0 Sun
People are trying to sleep ... Sun window
Testing 5:Sun 5 Sun
People are trying to sleep ... Sun window
Testing 8:Sun 8 Sun
PAGE SOMEONE - time is outside the Maintenance Window