我创建了一个简单的 linux 脚本,它本质上调用 sqlplus 并将结果放入变量 X。然后我分析 X 并确定是否需要发送 syslog 消息。
当我从命令行以“oracle”运行该脚本时,该脚本运行良好;但是,当我将 crontab 用作“oracle”并将其添加到我的工作中时,X 并没有被填满。
我可能是错的,但我相信问题是因为 cron 在静默模式下运行,X 实际上并没有被填满,但是当我手动运行它时。
这是我的 crontab -l 结果(作为 oracle):
0,30 * * * * /scripts/isOracleUp.sh syslog
这是我的完整脚本:
#Created by: hatguy
#Created date: May 8, 2012
#File Attributes: Must be executable by "oracle"
#Description: This script is used to determine if Oracle is up
# and running. It does a simple select on dual to check this.
DATE=`date`
USER=$(whoami)
if [ "$USER" != "oracle" ]; then
#note: $0 is the full path of whatever script is being run.
echo "You must run this as oracle. Try \"su - oracle -c $0\" instead"
exit;
fi
X=`sqlplus -s '/ as sysdba'<<eof
set serveroutput on;
set feedback off;
set linesize 1000;
select count(*) as count_col from dual;
EXIT;
eof`
#This COULD be more elegant. The issue I'm having is that I can't figure out
#which hidden characters are getting fed into X, so instead what I did was
#check the string legth (26) and checked that COUNT_COL and 1 were where I
#expected.
if [ ${#X} -eq 26 ] && [ ${X:1:10} = "COUNT_COL" ] && [ ${X:24:3} = "1" ] ; then
echo "Connected"
#log to a text file that we checked and confirmed connection
if [ "$1" == "syslog" ]; then
echo "$DATE: Connected" >> /scripts/log/isOracleUp.log
fi
else
echo "Not Connected"
echo "Details: $X"
if [ "$1" == "syslog" ]; then
echo "Sending this to syslog"
echo "==========================================================" >> /scripts/log/isOracleUp.log
echo "$DATE: Disconnected" >> /scripts/log/isOracleUp.log
echo "Message from sqlplus: $X" >> /scripts/log/isOracleUp.log
/scripts/sendMessageToSyslog.sh "PROD Oracle is DOWN!!!"
/scripts/sendMessageToSyslog.sh "PROD Details: $X"
fi
fi
以下是从终端作为 oracle 运行时的输出:
Wed May 9 10:03:07 MDT 2012: Disconnected
Message from sqlplus: select count(*) as count_col from dual
*
ERROR at line 1:
ORA-01034: ORACLE not available
Process ID: 0
Session ID: 0 Serial number: 0
这是我通过 oracle 的 crontab 作业运行时的日志输出:
Wed May 9 11:00:04 MDT 2012: Disconnected
Message from sqlplus:
对于系统日志:
PROD Details:
PROD Oracle is DOWN!!!
任何帮助将不胜感激,因为我是一个新的 linux 用户,这是我的第一个 linux 脚本。
谢谢!