2009-04-16 68 views
1

我有一个bash脚本,它检测在unix盒子上运行的失败系统组件。该脚本发送失败时的电子邮件。脚本每分钟运行一次(通过cron)。限制每15分钟发送一次警报的最简单方法是什么?限制脚本发送警报

我可以在发送提醒时创建/更新文件,只在文件的日期如此之多时才发送文件?

回答

2

像这样的东西可能会奏效:

stamp=/tmp/mystamp 
# create stamp file, if we haven't yet 
[ ! -f $stmp ] && touch $stamp 
tmp=$(tempfile) 

# see if 15 minutes has passed... 
diff=$(echo $(date -d "15 minutes ago" +%y%d%m%H%M) - $(date -d "$(stat $tmp |grep Change |cut -d: -f2-)" +%y%d%m%H%M) |bc) 
rm $tmp 

# if we last touched the stamp file less than 15 minutes ago 
# then quit 
[ $diff -le 0 ] && exit 0 

# update the stamp file 
touch $stamp 

# do your thing... 
echo 'Warning! Warning!' |mail -s "FOOBAR" [email protected] 
1

您可以为发送警报的脚本实现中介。不是直接发送电子邮件(通过sendmail,另一个邮件应用程序),你可以将它发送到另一个实际上会打电话的脚本。然后,此脚本可以跟踪(使用另一个文件)上次发送邮件的时间。这样,你可以检查你多久发送一次。 :)

0

如果您遇到的错误,触摸一个临时文件(比如/tmp/alert.email)。接下来在脚本中,检查文件是否存在,如果存在,分钟数是0,15,30,45,则发送电子邮件。我不确定这是否正是你想要的。基本上它:

  • 每隔15分钟发送一次电子邮件的次数不会超过一次。
  • 针对所有错误情况发送电子邮件,但不一定立即发送。
  • 可能会立即或长达14分钟后发送错误消息。

所以有一些像后:

#!/bin/bash 

MIN=`date '+%M'` 

if [ *ERROR_COND* ] ; then 
    touch /tmp/alert.email 
fi 

if [[ -f /tmp/alert.email && ($MIN = "15" || $MIN = "30" || $MIN = "45" || $MIN = "00") ]] ; then 
    # email here 
    rm -f /tmp/alert.email 
fi