1. 程式人生 > >Shell 指令碼實戰之報警監控

Shell 指令碼實戰之報警監控

Shell 指令碼實戰之報警監控

0. 需求

今天碰到的一個需求是:我們部門的人晚上需要看釘釘監控,手動排監控表很麻煩,於是想著使用shell 整一個指令碼,用以輪流通知大家監控。

1. 程式碼

#!/bin/bash
startDay=20181129 #the start day of calculate
str="A,B,C,D,E,F,G,H"

oldIFS=$IFS
IFS=,
arr=($str)

today=`date "+%Y%m%d"` 

for ((i = 0;i<1000;i++))
do
 tmp=`date -d "$startDay +$i days"
+"%Y%m%d"`
if [ $tmp -eq $today ] then res=`expr $i % 8` mes="今天值班的人是: ${arr[$res]}" curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxaaaabbbbccccdddzzz' \ -H 'Content-Type: application/json' \ -d ' {"msgtype": "text", "text": { "content": "'$mes'" } }'
; fi break done echo $mes #date -d"20140101 -1 days" +"%Y%m%d" 指定日期的加減法

3. crontab 呼叫

使用 crontab 進行每天呼叫一次即可。15 10 * * * /data/work/shells/notifyMonitor.sh