Here is mywhole script.
WhenToRun="01:00"
FilesToDelete="*log* *.log*"
FileLocation="$INTAS_INSTALL_DIR/envs/logs"
if [[ ! -d $FileLocation ]];then
echo "The FileLocation specified in Non-Existant"
fi
Progname=$(basename $0)
Usage="Usage:$Progname"'[-a <delete or backup>] [-l|-L <backup location>] [ -t <time>]'
IsCronJob=0
if [[ "$1" = "cron" ]]; then
. ~/.kshrc
IsCronJob=1
if [[ -z "$INTAS_INSTALL_DIR" ]]; then
echo "You must be logged in as the application owner to delete the middle tier logs"
return 1
fi
fi
BackupLocation="$INTAS_INSTALL_DIR/mt_backup"
if [[ ! -d $BackupLocation ]];then
mkdir -p $BackupLocation
fi
TEMPDIR=/tmp
if [[ -d $INTAS_INSTALL_DIR/envsw/tmp ]]; then
TEMPDIR=$INTAS_INSTALL_DIR/envsw/tmp
fi
while getopts a:l:L:t:d optvar
do
case $optvar in
a)option="$OPTARG"
if [[ $option = "delete" ]]; then
echo "Deleting the middletier logs"
cd $FileLocation
list=$(ls *log*)
for files in $list
do
rm -f $files
touch $files
echo " Middletier is cleaned up"
done
fi
if [[ $option = "backup" ]]; then
echo "Backuplocation is must to specify"
fi
;;
l) BackupLocation="$OPTARG"
if [[ ! -d $BackupLocation ]];then
echo "Specify a existing directory"
fi
if [[ $BackupLocation != *backup ]]; then
echo "Appending backup subdirectories"
BackupLocation=$BackupLocation/backup
mkdir -p $BackupLocation >/dev/null 2>&1
if [[ $? != 0 ]];then
echo "First Create The Directory And Then Take backup"
fi
cd $FileLocation
list=$(ls *log*)
cp -R $FilesToDelete $BackupLocation
for files in $list
do
rm -f $files
touch $files
done
else
mkdir -p $BackupLocation >/dev/null 2>&1
list=$(ls *log*)
cp -R $list $BackupLocation
for files in $list
do
rm -f $files
touch $files
done
if [[ ! -d $BackupLocation ]]; then
echo "Unable to make backup directory: $BackupLocation"
fi
fi
l_flag=Y
Llcron=l
;;
L) BackupLocation="$OPTARG"
if [[ ! -d $BackupLocation ]];then
echo "You specified a non-existant directory"
fi
if [[ $BackupLocation != *backup ]]; then
echo "Take backup as a 7 day rotation"
newlocation="basename $BackupLocation"
BackupLocation=$BackupLocation/$newlocation.$(date +%a)
mkdir -p $BackupLocation >/dev/null 2>&1
if [[ $? != 0 ]]; then
echo "Create a Rotaional directory and take backup"
fi
cd $FileLocation
list=$(ls *log*)
cp -R $list $BackupLocation
for files in $list
do
rm -f $files
touch $files
done
else
if [[ $BackupLocation = *backup ]]; then
BackupLocation=$BackupLocation/$newlocation.$(date +%a)
mkdir -p $BackupLocation >/dev/null 2>&1
cd $FileLocation
list=$(ls *log*)
cp -R $list $BackupLocation
for files in $list
do
rm -f $files
touch $files
done
fi
if [[ ! -d $BackupLocation ]]; then
echo "Unable to make backup directory: $BackupLocation"
fi
fi
L_flag=Y
Llcron=L
;;
t) WhenToRun="$OPTARG"
if [[ $WhenToRun = "never" ]]; then
WhenToRun='24:00'
fi
if [[ $WhenToRun = "now" ]]; then
IsCronJob=1
WhenToRun='24:00'
fi
if /bin/echo $WhenToRun | egrep '^[0-9]{1,2}:[0-5][0-9]$' >/dev/null; then
: Time is correct format
else
echo "Incorrect time format, time must be in HH:MM format"
fi
;;
d) DEBUGGING='/bin/echo'
;;
d) DEBUGGING='/bin/echo'
;;
*) echo $Usgae
return 1
;;
esac
done
(( optcount = OPTIND - 1 ))
shift $optcount
if [[ -z "$1" ]]; then
echo $Usage
echo
echo 'This command will schedule an Middleztier cleanup at a certain time of the day'
echo 'The time is given with the -t option and its default is 1am'
echo "A time of 'now' means to execute the cleanup operation now"
echo "A time of 'never' means that the cleanup operation should not be scheduled"
echo 'A backup location can be specified with the -l option. This is the directory'
echo 'where files are moved that are older than 1 day. The default is '"$INTAS_INSTALL_DIR/mt_backup."
echo 'Instead of -l, you can use -L to specify a 7-day backup directory rotation.'
echo
echo "More than one cleanup can be scheduled as long as each cleanup is different"
echo "Typical usage: $Progname -a backup/delete -l|L location time "
fi
if [[ "$L_flag" = "Y" && "$l_flag" = "Y" ]]; then
echo 'You can not specify both -l and -L options.'
fi
if [[ ! -d $BackupLocation && -w $BackupLocation ]]; then
echo 'The backup location does not exist or is not writable'
fi
cd /
if whence $Progname >/dev/null; then
: We found this program in the path
else
echo "Unable to locate $Progname in the PATH, check the .kshrc for correctness"
fi
arg1=$1
arg2=$2
cd $INTAS_INSTALL_DIR/envsw
if [[ $? != 0 ]]; then
echo "Unable to cd to $INTAS_INSTALL_DIR/envsw, exiting..."
fi
echo "Start middletierclean at $(date)"
if [[ $IsCronJob -ne 1 ]]; then
# We are not a cron job, install a line into the crontab
if (crontab -l 2>&1 1>/dev/null | grep 'not authorized'); then
echo "Unable to use cron, contact your system administrator"
echo "Check that $(whoami) has an entry in cron.allow"
return 1
fi
cronfile="$TEMPDIR/cur_crontab$$"
crontab -l 2>&1 | grep -v '^crontab' >$cronfile
if grep "$Progname cron .* $arg1 $arg2" $cronfile >/dev/null; then
tmpvar=$(grep -v "$Progname cron.*$arg1 $arg2" $cronfile)
echo "$tmpvar" >$cronfile
fi
if [[ $WhenToRun != 24:00 ]]; then
Llcron=l
minute=$(/bin/echo $WhenToRun | cut -d: -f2)
hour=$(/bin/echo $WhenToRun | cut -d: -f1)
if [[ -z "$DEBUGGING" ]]; then
echo "$minute $hour" '* * *' ". ~/.kshrc; $(whence $Progname) cron -e $FileLocation -$Llcron $BackupLocation $arg1 $arg2" >>$cronfile
else
echo "$minute $hour" '* * *' ". ~/.kshrc; $(whence $Progname) cron -d -e $FileLocation -$Llcron $BackupLocation $arg1 $arg2" >>$cronfile
fi
fi
crontab $cronfile >/dev/null 2>&1
if crontab -l | diff - $cronfile >/dev/null; then
if [[ $WhenToRun != 24:00 ]]; then
echo crontab has been installed correctly
else
echo The crontab entry for \"$arg1 $arg2\" has been removed
fi
fi
else
echo "Error installing crontab file"
fi
/bin/rm -f $cronfile
echo "End middletierclean at $(date)"i want to run it as --
It can be run as--
mt_clean -a backup -l|L <locationof backup> -t <time>
eg
mt_clean -a backup -L /abc/def -t 02:20
here everything is working fine but it is not taking the cron time that is 02:00 or whatever time i mention.
Please help me out in this.It's very urgent for me.
Can anyone suggest me how can i modify this so that it will pick up the cronjob and run when the time is specified.