我一直在使用cronic使作业成功完成时使来自cron作业的电子邮件静音。我试图对其进行自定义,以便当响应代码为0且错误输出与字符串“ mount: /VessRAID/RH: /dev/sde1 already mounted on /VessRAID/RH.
”匹配时,不发送电子邮件。下面是脚本,然后是电子邮件的内容,然后是我试图抑制不起作用的电子邮件的尝试。知道我做错了什么吗?
#!/bin/bash
# Cronic v3 - cron job report wrapper
# Copyright 2007-2016 Chuck Houpt. No rights reserved, whatsoever.
# Public Domain CC0: http://creativecommons.org/publicdomain/zero/1.0/
set -eu
TMP=$(mktemp -d)
OUT=$TMP/cronic.out
ERR=$TMP/cronic.err
TRACE=$TMP/cronic.trace
set +e
"$@" >$OUT 2>$TRACE
RESULT=$?
set -e
PATTERN="^${PS4:0:1}\\+${PS4:1}"
if grep -aq "$PATTERN" $TRACE
then
! grep -av "$PATTERN" $TRACE > $ERR
else
ERR=$TRACE
fi
if [ $RESULT -ne 0 -o -s "$ERR" ]
then
echo "Cronic detected failure or error output for the command:"
echo "$@"
echo
echo "RESULT CODE: $RESULT"
echo
echo "ERROR OUTPUT:"
cat "$ERR"
echo
echo "STANDARD OUTPUT:"
cat "$OUT"
if [ $TRACE != $ERR ]
then
echo
echo "TRACE-ERROR OUTPUT:"
cat "$TRACE"
fi
fi
rm -rf "$TMP"
这是电子邮件通知的样子:
Cronic detected failure or error output for the command:
/usr/local/sbin/reg-backup-cronic.sh daily
RESULT CODE: 0
ERROR OUTPUT:
mount: /VessRAID/RH: /dev/sde1 already mounted on /VessRAID/RH.
STANDARD OUTPUT:
/dev/sde1 on /VessRAID/RH type ext4 (rw,relatime)
这是我尝试的包装脚本:
#!/bin/bash
/usr/local/sbin/reg-backup.sh $1
CODE=$?
err=$TRACE
if [[ $CODE -eq 0 && $err = "mount: /VessRAID/RH: /dev/sde1 already mounted on /VessRAID/RH." ]]
then
exit $CODE
fi
A,继续发送电子邮件。
答案 0 :(得分:0)
向cronic的创建者Chuck Houpt致敬的提示为我提供了一个答案,该答案是查看原始脚本以及错误发生的原因。区分大小写是我最好的:
if mount | grep Vessraid; then
echo starting $1 backup >> /var/log/vessraid.log
请注意,VessRAID中的情况应该是:
if mount | grep VessRAID; then
echo starting $1 backup >> /var/log/vessraid.log
现在,只有在确实存在错误时,才会发送电子邮件。