Wednesday, 5 November 2014

Intimating crontab changes

#!/bin/bash
crontab -l > cronlist
if ! diff cronlistold cronlist > diffs
    then

    cat diffs | grep '<' > removed && remove=`wc -l removed | sed 's/removed//g'` && [ ! "$remove" ] && remove=0
    if [ $remove > 1 ]
    then
     echo "The following lines are removed from old entry" > total
     cat removed | sed 's/<//g' | grep -v tmp >> total
     echo >> total
    fi
   
    cat diffs | grep '>' > added && add=`wc -l added | sed 's/added//g'` && [ ! "$add" ] && add=0
    if [ $add > 1 ]
    then
    echo "The following lines are added newly to crontab" >> total
    cat added | sed 's/>//g' | grep -v tmp >> total
    fi

fi

cat total 2> /dev/null

tot="`cat total  2> /dev/null | wc -l `" && [ ! "$tot" ] && tot=0

if [ $tot -gt   0 ]
    then
    cat total 2> /dev/null
    cat total | mail -s "Crontab changes" praveensam.s@lhsgroup.com
fi
mv cronlist cronlistold && rm -rf total