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.

Recommended Answers

All 23 Replies

Hi unix guru's,

i did not get even a single response till now regarding my quesry ie-

can nayone please suggestt me how i can implement the cronjob part inside a script like it is mentioned in the above script.i am not a unix guy and got into this problem.i will appericiate if anyone giveme me some clues also.

please explain me how the cronjob part can be implemented inside a script.i just want to run the file and the script will do the rest.
one more problem is there the backup is not being taken at the default location if no location is specified.

if you need any clarification then let me know but suggest me something so that i can proceed further.
Thanks

so without the "WhenToRun" setting, the script just works?

Yes it is working properly.the problem is it is not taking the time which i specify.the script can be run as

mt_clean -a backup/delete -l|L <location> -t 02:00(eg)

but as soon as i run it it is taking the backup but not at the specified time.Cronjob part is not working properly.i guess i need to modify the script but how to do that i am not getting,so i need your help in this regard.

Thanks

why don't you just run the crontab command to add the script to the cron joblist? why do you need the script to add itself?

Yes that is an option, but It is required in the script which i ned to write.
I have to do the cronjob part inside the script itself.

I would create a second script that would handle the crontab insertion, and call that from within this script. not so sure about bash, but crontabbing a script from that same script is a bit too recursive for me :)

I appericiate your help.I wil wait for your response.

Hi Dima,

did you write the script that will help me in this regard.

I said I WOULD create a script, not that i WILL write it for you

I said I WOULD create a script, not that i WILL write it for you

No Problem Dima,i appericiate your help.You mean to say you will help me in rectifying the script right.That is also fine to me.

there is nothing to rectify there if the script works. what I suggested for you to try, is to take the cron insertion function out of the backup script and create a second script - CronInsert.sh, that would take the backup script and would put it into crontab

CAn you do me a favour if you can help me out in creating that crontab part.i am new to unix and to shell scripting.i created the script as i have a similar script but that is doing diffrent things.i will apperciate your help in this.

in the backup script file collect minute, hour, day, month, weekday
add the crontab -e command there (this will create the crontab file, if it doesn't exist.
after that add:
/some/dir/CronInsert.sh $minute $hour $day $month $weekday
--------------------------------------------

CronInsert should be something like

echo "$1 $2 $3 $4 $5 /some/dir/BackupScript.sh >/tmp/backupcron.log" >>/etc/crontab

if you have user specific cron entries:

crontab -u $USER -l > crontab.file.$USER
echo "$1 $2 $3 $4 $5 /some/script.sh" >> crontab.file.$USER
crontab -u $USER crontab.file.$USER

in the backup script file collect minute, hour, day, month, weekday
add the crontab -e command there (this will create the crontab file, if it doesn't exist.
after that add:
/some/dir/CronInsert.sh $minute $hour $day $month $weekday
--------------------------------------------

CronInsert should be something like

echo "$1 $2 $3 $4 $5 /some/dir/BackupScript.sh >/tmp/backupcron.log" >>/etc/crontab

Can you explain me in reference to my script that will much more helpfull.Where should i use these lines.
add the crontab -e command there -> i know in the file but where in the file.

/some/dir/CronInsert.sh $minute $hour $day $month $weekday ->what this line isgoing to.

can you elaborate all this is respect to my file,that way i can get much more and do the things.

I ned one more help,
if i run the script through cron and do not specify any backup location the copy should happen at the default backup location that is not happening only it is creating the default backup location.can you try to help me out in this also.
Thanks

you should know your own script, it is much easier for me to write my own script than to go through a hundred lines of code.

I ned one more help,
if i run the script through cron and do not specify any backup location the copy should happen at the default backup location that is not happening only it is creating the default backup location.can you try to help me out in this also.
Thanks

well, set it up with a default backup location, present it in question and if nothing is entered, keep it in the variable you pass over to cron

in the backup script file collect minute, hour, day, month, weekday
add the crontab -e command there (this will create the crontab file, if it doesn't exist.
after that add:
/some/dir/CronInsert.sh $minute $hour $day $month $weekday
--------------------------------------------

CronInsert should be something like

echo "$1 $2 $3 $4 $5 /some/dir/BackupScript.sh >/tmp/backupcron.log" >>/etc/crontab

Can you give me some example in regard to this.This will be very helpfull.

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 -$Llcron $BackupLocation $arg3" >>$cronfile
else
echo "$minute $hour" '* * *' ". ~/.kshrc; $(whence $Progname) cron -d -e -$Llcron $BackupLocation $arg3" >>$cronfile
fi

script1.sh:

ècho Enter minute:
read minute
echo Enter hour:
read hour
/some/dir/script2.sh $minute $hour

script2.sh:

echo "$1 $2 * * * /some/dir/BackupScript.sh >/tmp/backupcron.log" >>/etc/crontab

Hi Dima,

any solution for the default backup location problem which i mentioned just above your last thread.

where is the default location, and how do you acquire an alternative?

where is the default location, and how do you acquire an alternative?

Dima,

if you can look in the script which i wrote,at the beginning of this thread,i am implementing the functionality of copying inside the backuplocation itself actually we dont need this.
i need to write it like
if mention as now it should take the backup now otherwise when i schedule the cron part.

i tell you my actual requirement--

1)Utility will be run under Specific owner.
2) This utility will clean files in $INTAS_INSTALL_DIR/envsw/logs. And following logs will be backed up or deleted.

All Logs and Core files
3) mt_clean -a<action> [-t <time>]  -L <backup location>

-a<action> - has two values "delete" or "backup”. If not specified by default "delete" action will occur. If "backup" option is given, files will be backed up on the location specified by the -L, -L is mandatory if back up option is specified.

-t<time>  - The time is given with the -t option and its default is 1am

A time of 'now' means to execute the cleanup operation now
A time of 'never' means that the cleanup operation should not be scheduled and any existing cleanup should be cancelled.

-L <backup location> - this field is mandatory if backup action is selected.

Note: As middle tier process are running and not stopped while this utility is running, the utility should create an empty file with same name which has deleted or backed up. Otherwise middle tier logs will not be generated until the middle tier process is restarted. Any core files will be backed up or deleted according to the action chosen. 

i have created the script according to the req but its not doing anything for me so need your help,I will thansk alot if you can provide me a sample script for this.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.