moving log file into Archive directory

Hi All,

I'm trying to write a script which will do following :

  • For any old log under trace directory, if found move it to Archive
  • Check for a process �process-A� if it is running abort from the script
  • Else start it up (start_process-A.sh this case)
  • If it fails to start the process, exit with the appropriate exit code
  • All of the above steps will be logged in the log file under trace
#!/bin/ksh
SERVICE='process-A'
JOB=`basename $0`
OWNPID=$$
LOGFILE=$HOME/trace/$JOB$OWNPID.log
Archive=$HOME/trace/Archive
 
archive()
{
if [ -f $HOME/trace/ start_process-A.sh.*.log ]; then
  mv $HOME/trace/ start_process-A.sh.*.log $Archive
else
  echo "No file to archive" | tee -a ${LOGFILE}
  exit 1;
fi
}

>>>> The above function moves all the files to the Archive directory as I used wildcard (*) but I want to keep the latest file (which will be generated upon completion of this script) in the trace directory and rest in the Archive directory. I have done lots of search in the google and just cannot find it. Any help will be appreciated.

# Checking process-A .. if not found - equals to 1, start it
ps -ef | grep $SERVICE | grep -v grep
if [ $? -eq 1 ]
then
  echo "`date` : $SERVICE is down, starting up ..." | tee -a ${LOGFILE}
  $HOME/ start_process-A.sh | tee -a ${LOGFILE}
else
  echo "`date` : $SERVICE is up ..aborting from the script" | tee -a ${LOGFILE}
fi
#Moving old log file into Archive directory
archive

>> Is there a was to check the start_process-A.sh script is failed or hung? if so how do I redirect it to the log file?

For the future please use [CODE] tags around the logic. Please review the forum rules.

Let's assume(might not a good assumption) that start_process-A.sh will return a non-zero return code on error. You can check the return code just like you did with the grep. This gets more complicated when trying to check the return code from a program in the pipeline. Do you really need use tee? Can you just append(>>) to ${LOGFILE}? If you really need the output in the current script and the log file then you have to be more creative. I will wait for you response before going down that route. Do you know if the shell is ksh93?