Hi,
I have a bash script that reads the directories from a jboss/server directory in
order to start specified server instances. Part of the script archives older logs and creates new ones if required. To do this, it reads the array of directories and in a loop archives, and creates new ones depending on the file size and existence.
The new files are are named as <array_entry>.log which works fine except the last array_entry is always created as <array_entry>.log.log I have experimented by removing and adding entries to the array and it always creates the last entry with a .log.log extension.
The loop is like this:
function processLogs {
printf "\n"
printf "Checking the log files exist and are writeable \n"
printf " \n"
printf "contents of $LOG_DIR are: \n"
ls $LOG_DIR
printf "\n"
# check to see there are any run.sh process running
if [ `/bin/pidof -x run.sh | wc -l` = 0 ]
then
for log in "${dir_array[@]}".log
do
if [ -e "$LOG_DIR/$log".log ]
then
# file exists, so archive it and create a new version
mv -f $LOG_DIR/$log.log $LOG_DIR/archive/`eval date +%Y%m%d-%k:%m`-$log.log
printf "Moved $log.log to archive/$log.log \n"
# let's make it non-writeable too so it can't be re-used by accident
chmod -w $LOG_DIR/archive/`eval date +%Y%m%d-%k:%m`-$log.log
touch $LOG_DIR/$log.log # create new files
chmod a+w $LOG_DIR/$log.log
elif [ ! -e "$LOG_DIR/$log".log ]
then
# log file doesn't exist so create and make it writeable
printf "Log file $log.log doesn't exist, creating it \n"
touch $LOG_DIR/$log.log
chmod a+w $LOG_DIR/$log.log
elif [ ! -w "$LOG_DIR/$log".log ]
then
# log file exists but isn't writeable
# printf "logfile $log isn't writeable, correcting it \n"
chmod a+w $LOG_DIR/$log.log
fi
done
else
printf "there are running processes so left log files alone \n"
#printf "processes are: \n"
#ps -ef | grep "run.sh" | grep -v grep | awk '{print $2}'
fi
printf "contents of $LOG_DIR are: \n"
ls $LOG_DIR
printf "\n"
}
Obviously the array and other variables are set further up, the array is definitely filled. This is bash on Ubuntu 10.04.
[edit] just to be clear, the touch $LOG_DIR/$log.log command in both the archive and create elif conditions do the same
thing, ie create the last .log.log file.
Thanks