Team,
I have a shell script which will b run in cron job. On an adhoc basis.
The script needs to be run such that the moment it goes to the particular directory.
It should adapt to its ownership. But the script which i had shared below is run by a single user having admin privilege.. would it be possible for you to share with me such that it uses it own file ownership ?...
File ownership of the particular directory as below:
1)Trigger/archive directory:is owned by PSAPWCC user
2)XS-SuperSwitch/archive:is owned PSAPADM user
3)logs/archive:is owned PSAPROA user
Script details as below:
Name of the script:SCANIT_File-System_Tidy.sh
# This script will be used to delete the data which is older than 60days
# from Archive folders Trigger/archive, XS-SuperSwitch/archive, logs/archive
#
####################################################################
Initialize() {
##################################
#
# Set variables
#
##################################
LOG="/tmp/SCANIT_File-System_Tidy.log.${HOSTNAME}.${date_stamp}.$$"
TMPLIST=/tmp/SCANIT_tmplist.$$;
KEEP_LIMIT=60
SCANIT_WLOAD=`( cd \`dirname $0\`; pwd | cut -f3 -d/)`
TRIGGER_PATH=/wload/psap/app/scanit/Trigger/archive
XS-SuperSwitch_PATH=/wload/psap/app/scanit/XS-SuperSwitch/archive
Recon_PATH=/wload/psap/app/scanit/recon/process/logs/archive
}
DeleteFiles() {
PATH=$1;
RETAIN=$2;
do
/usr/bin/find . -type f -mtime +${RETAIN} -name "${PATH}files.*.gz" > ${TMPLIST};
tmpCount=`cat ${TMPLIST} | wc -l`;
if [ $tmpCount -eq 0 ]; then
LogThis "No ${RETAIN} Day-old Archived Files to Remove";
fi
else
cat ${TMPLIST} | while read TAR_line
do
/usr/bin/rm -e -f $TAR_line >>${LOG} 2>&1;
CheckRetStat "Removed $TAR_line from ${PATH}" "Unable to Remove $TAR_line from ${PATH}" 1;
done
done
}
#######################################################################
#
# Function for Logging
#
#######################################################################
LogThis() {
# $1 = Message
echo "`date +%m/%d/%y` `date +%H:%M:%S` -- $1" >> ${LOG};
#echo "$1" >> ${LOG};
}
#######################################################################
#
# Function for Checking the Return Value of the previous command
#
#######################################################################
CheckRetStat() {
RetStat=$?;
SuccessMsg=$1;
ErrorMsg=$2;
ExitIfErr=$3;
if [ $RetStat -ne 0 ]; then
LogThis "***** ERROR - $ErrorMsg";
# Exit Script if the ExitIfErr value is 1";
if [ $ExitIfErr -eq 1 ]; then
WriteAlert 14112;
exit $RetStat;
fi
else
LogThis "SUCCESS - $SuccessMsg";
fi
}
###################################
#
# Script starts here
#
###################################
Initialize;
# Ensure that the script is ran as the SCANIT user
if [[ `whoami` != "${SCANIT_WLOAD}adm" ]];then
LogThis "Please run this as the ${SCANIT_WLOAD}adm user.";
LogThis "Exiting...";
exit 1;
fi
PID=$$;
. ~/.profile
#Ensure that there are no other SCANIT housekeeping process running
ps -ef | grep "SCANIT_File-System_Tidy.sh" | grep -v grep > /tmp/tempfile
if [[ ! -x /tmp/tempfile ]]; then
AnotherInst=`cat /tmp/tempfile | grep "${SCANIT_WLOAD}glm" | grep -v " $PID " | awk '{ print $2 }'`;
fi
if [[ ! -z $AnotherInst ]]; then
LogThis "Process ID for this run: $PID";
LogThis "Another instance: $AnotherInst";
echo $AnotherInst | while read NPID
do
LogThis "`ps -ef| grep $NPID`";
done
LogThis "Another Instance of the Script for this environment is running!";
LogThis "Exiting...";
exit 1;
fi
>${LOG};
chown ${SCANIT_WLOAD}glm:${SCANIT_WLOAD}user ${LOG}
WriteAlert 14110;
LogThis "Starting SCANIT File-System Tidy Log";
LogThis "`hostname`-`date +\%d\%m\%C\%y'-'\%H\%M\%S`";
DeleteFiles $TRIGGER_PATH $KEEP_LIMIT;
DeleteFiles $XS-SuperSwitch_PATH $KEEP_LIMIT;
DeleteFiles $Recon_PATH $KEEP_LIMIT;
LogThis "Removing temporary files...";
/usr/bin/rm -e -f ${TMPLIST} >>${LOG} 2>&1;
/usr/bin/rm /tmp/tempfile;
LogThis "COMPLETED SCANIT File-System Tidy";
LogThis "SCANIT Script executed `date`";
WriteAlert 14111;
Many Thanks!
Whizkid