miércoles, 14 de diciembre de 2011

Checking the status of an Oracle Instance in Unix

In many Unix script is very common to find the commands below to check the status of an Oracle Instance :

_status=`ps -fe | grep pmon_${ORACLE_SID} | grep -v grep | wc -l`

This usually is correct. If the database is Up then it will return 1, else it returns 0.

But what happens if there's two instances in the same server. One named ORCL and the other ORC. In this case the command will return 2 when both databases are Up.

A posible solution to avoid a mistaken behavior in the script when asking "if [ ${_status} -eq 1 ]; then", is to check the status of the process relative to the specific instance. It can be done this way:

_status=`ps -fe | grep pmon_${ORACLE_SID} | grep -v grep | awk '{ print $NF"*" }' | grep pmon_${ORACLE_SID}"*" | wc -l`

With the commands above it will always return 0 or 1.

sábado, 3 de diciembre de 2011

Problem with dinamic array in ksh script

The other day I was working on a unix script in which I use dynamic arrays. I found some strange behavior when trying to fill up the array. When I use the code with “while loop” the array didn’t fill up.

  n=0
  grep -i "fbackup(" logfile.log | while read line
  do
   errores[$a]="$line"
   (( n=n+1 ))
  done


When the above code was changed to use the “for loop”, it did worked.

  ORIGIFS=$IFS
  IFS=`echo -en "\n\b"`     # set $IFS to end-of-line
  n=0
  for linea in `grep -i "fbackup(" logfile.log`
  do
   errores[$n]=${linea}
   (( n=n+1 ))
  done
  IFS=$ORIGIFS      # set $IFS back