Script to send alert if any changes are made in crontab.

Hi i want to know how can i write a script to check if any changes are made and send an alert in crontabs . i am using .ksh file extension for writing scripts.

maybe you could try something like this.

x=$(crontab -l >/tmp/crXc)
sleep 1800 #(wait for half-hour )
y=$(crontab -l >/tmp/crXy)
if [[ ! -z $(comm -3 $x $y) ]] ; then echo "crontab entry(s) is maybe changed .." ; 
echo -e "$(date "+%F %H:%M:%S") in\n --new crontab--\n`more $x` \n\nhalf an hour ago\n --old crontab--\n`more $y`"; fi
rm -f $x $y
 

regards
ygemici

You could also run something like this as a root crontjob - it checks any cronttab files modified since it was last run and reports on them:

CRONDIR=/var/spool/cron/crontabs
CKFILE=/tmp/last.crontab.check
ALERT=someuser@your.alerthost
if [ -f $CKFILE ]
then
    find $CRONDIR -type f -newer $CKFILE | while read tabfile
    do
       echo "Crontab file for user $(basename $tabfile) has changed" | mail -s "Crontab changed" $ALERT
    done
fi
touch $CKFILE

Hi guys thanks for replying. :slight_smile:
will try with thr scripts.