Operating System - HP-UX
1826164 Members
4242 Online
109691 Solutions
New Discussion

Re: Trace on Crontab job.

 
SOLVED
Go to solution
harry_7
Frequent Advisor

Trace on Crontab job.

Dear All,

Do some one have any script to monitor the cron job. Where you can see any modification in the crontab file. Means some one has added or removed the entries.

I am trying to do this way

#!/usr/bin/ksh
. ~/.profile
###############################################
#
#
###############################################
TO=harpreet_singh@ctl.creative.com
LOGDIR=/usr/local/bin/perf-monitor
SUBJ="New Entry Found in the Crontab"
LOGFILE="${LOGDIR}/crontab_`hostname`.`date +'%d%m%y'`"

#crontab -l > master_cron_list
crontab -l > $LOGFILE
diff master_cron_list $LOGFILE > diff.txt

(
cat << !
cat $LOGFILE/diff.txt
echo "Some changes has been made in Crontab"
date
To : ${TO}
Subject : ${SUBJ}
Cc : ${CC}
Bcc : ${BCC}
!
) | /usr/lib/sendmail -f ora_sysadmin@ctl.creative.com ${TO} ${CC} $BCC
echo "No Problem"
------------

Here I can send the mail, but recipient’s name, subject is also missing.
How I can show the diff.txt file in email
How I can attach diff.txt file in email.

Regards, Harry
4 REPLIES 4
Steven E. Protter
Exalted Contributor

Re: Trace on Crontab job.

Try putting a set -x right after the shell in the actual script cron calls.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
RAC_1
Honored Contributor

Re: Trace on Crontab job.

Put an entry though cron through root user. This script will check crontab file size at defined interval and will execute diff on it. (May be also file size change) and if finds something it will email it to you.

Something like follows.

#!/usr/bin/ksh

file_to_watch=/var/spool/cron/crontab/user_name
cp ${file_to_watch} /some_location/.
new_file=/some_location/file_name
size=$(ll /var/spool/cron/crontab/user_name|awk '{print $5}')
new_size=$(ll /var/spool/cron/crontab/user_name|awk '{print $5}')
if [[ ${new_size} -gt ${size} ]]
then
changes=$(diff $file_to_watch $new_file)
echo $changes | mailx -s "Change in Cron" xxx@dd.com
fi
There is no substitute to HARDWORK
Victor Fridyev
Honored Contributor
Solution

Re: Trace on Crontab job.

Hi,

Try something like this:
diff master logfile > diffile
if [ -s diffile ]; then
mailx -s "cron has been changed" harry@yourdomain < diffile
fi


HTH
Entities are not to be multiplied beyond necessity - RTFM
harry_7
Frequent Advisor

Re: Trace on Crontab job.

Dear all,

Thanks for the reply. I got the answer.

Regards

Harry.