gpt4 book ai didi

linux - 使用 diff 检查 crontab 更改的脚本

转载 作者:太空宇宙 更新时间:2023-11-04 03:55:22 24 4
gpt4 key购买 nike

我需要一个脚本,它需要以一种方式查看文件中当前 crontab 的副本,然后每天都需要获取 cron tab 副本,并且需要使用“diff”命令进行比较,如果不匹配,则需要发送警报邮件。有人可以帮助我吗?

目前我正在使用下面的脚本,但是这个脚本的问题是即使 crontab 中所做的更改是正确的,它也会发送警报。但是我想使用 diff 命令比较内容。所以这个脚本不适合我的要求

#!/bin/sh
export smtp=smtprelay.intra.coriant.com:25
CROND=/home/ssx00001
ALERT=redmine@coriant.com
checkf=last.crontab.check
if [ -f $checkf ]
then
find $CROND -type f -newer $checkf | while read tabfile
do
echo "Crontab file for Redmine has changed" | mail -s "Crontab changed" $ALERT
done
fi
touch $CHECKF

最佳答案

#!/bin/sh
export smtp=smtprelay.intra.coriant.com:25
ALERT=redmine@coriant.com
crontab -l > /home/ssx00001/y.txt
cat y.txt
diff /home/ssx00001/x.txt /home/ssx00001/y.txt > /home/ssx00001/z.txt
ab=`cat z.txt | wc -l`
echo $ab
if [[ $ab != 0 ]]; then
echo "Crontab for Redmine has changed" | mail -s "Crontab modified" $ALERT
fi

(/home/ssx00001是文件存储的路径?)还在/home/ssx00001 中创建一个文件 x.txt,其中包含当前 cronjobs 的数据

关于linux - 使用 diff 检查 crontab 更改的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24931671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com