以非静默模式运行cron作业?

时间:2012-05-09 17:30:18

标签: cron sqlplus

我创建了一个简单的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:

到syslog:

PROD Details:
PROD Oracle is DOWN!!!

任何帮助都会受到赞赏,因为我是一个新的linux用户,这是我的第一个linux脚本。

谢谢!

1 个答案:

答案 0 :(得分:1)

我的Oracle数据库技能非常有限,但您不需要设置ORACLE_SID和ORACLE_HOME吗?

从命令行检查这些变量,并在cron中设置这些变量并重试。