首页 > 解决方案 > 在shell脚本的while循环中执行sqlplus时控制不返回UNIX

问题描述

我观察到一些 shell 脚本卡住了,因为 sqlplus 会话没有完成它的会话。这种行为是间歇性的。我将给出其中一种情况。下面是执行一个 sql 查询的代码。

 if [[ ${rcloop1} -eq 0 ]]; then
      sql_cnt_loop=0      
      # perform 2nd loop after 1 second to check if Indexing has completed
      sleep 1
      while : ; do
         exec_SQL "SELECT count(*)    \
                     FROM server_states  \
                    WHERE topic = 'WLM_FPFA_INDEXES'  \
                      AND current_state <> new_state \
                      AND server_name in ('${E_WLM_BATCH_SERVER_NAME_1}', '${E_WLM_BATCH_SERVER_NAME_2}')"
         rcloop2=$?

         if [[ $rcloop2 -eq 0 ]]; then
            sql_cnt_loop=$sql_cnt_loop+1
            if [[ ${E_WLM_SQL_RESULT} -eq 0 ]]; then
               Log "DJ Indexing has Completed..."
               rcloop2=0 # no rows - indexing has completed (not found in server_states table), return RC=0
               break     # exit from 2nd loop
            elif [[ ${E_WLM_SQL_RESULT} -gt 0 ]]; then
               sleep 1 # wait 1 sec then loop again
               continue  # record exists, continue monitoring indexing - loop again
            fi

         fi
      done

      rc=$rcloop2

   else
      rc=$rcloop1
   fi

下面是unix会话。pwlm 8297 8290 0 00:16?00:00:03 /bin/ksh -a /opt/XWLMLL02 pwlm 29121 8297 0 00:32?00:00:00 sqlplus -s

脚本内部已经运行了一个进程,上面的代码部分是为了监控进程是否完成。即使该过程完成,脚本也没​​有从这一点进一步移动。进一步检查后,我看到了一个活动的 sqlplus 会话。我怀疑 sqlplus 会话来自上面的代码,并且它以某种方式被卡住了,脚本卡住了。

下面是实际的函数 exec_SQL。

exec_SQL()
{
   sql_Stmt=$1

   sql_Type=`echo $sql_Stmt | awk -F " " '{ printf toupper($1) }'`

   E_WLM_SQL_RESULT=`sqlplus -s /@${E_WLM_DB} <<!
      set pagesize 0 feedback off verify off heading off echo on
      whenever SQLERROR exit SQL.SQLCODE
      whenever OSERROR exit 9
      $sql_Stmt;
      commit;
   !`

   sql_RC=$?

   if [[ $sql_RC -eq 0 ]]; then

      str_Result=`echo $E_WLM_SQL_RESULT | grep SP2`  

      if [[ ${#str_Result} > 0 ]]; then
         sql_RC=57      # SQL Error
         sql_Code=0 
      else
         export E_WLM_SQL_RESULT
      fi
   else
      sql_Code=$sql_RC   # Store SQL.SQLCODE before overwriting

      if [[ $sql_RC -eq 9 ]]; then   # OSERROR
         sql_RC=91     
      else
         case $sql_Type in 
            INSERT) sql_RC=52 ;;
            UPDATE) sql_RC=53 ;;
            SELECT) sql_RC=54 ;;
            DELETE) sql_RC=56 ;;
            *) sql_RC=57 ;;
         esac
      fi
   fi

   if [[ $sql_RC -ne 0 ]]; then
      Log "Error in executing SQL Statement: $sql_Stmt "
      Log "SQL.SQLCODE=$sql_Code"
      Log "=====================================   ORACLE ERROR DESCRIPTION  ====================================="  
      Log "$E_WLM_SQL_RESULT"
      Log "======================================================================================================="
      Log "Returning with $sql_RC"

   fi

   return $sql_RC 
}

在过去的 7 天里,我已经观察了 3 次类似的问题。但是,它适用于不同的脚本。逻辑保持不变。

该脚本在我们有 AIX 服务器的旧基础架构中运行良好。我们在那里根本没有遇到过这个问题。

任何人都可以帮我解决这个问题吗?

提前致谢。

标签: linuxshellwhile-loopsqlplus

解决方案


推荐阅读