当前位置: 代码迷 >> Oracle开发 >> crontab 定时施行shell脚本,脚本里的sql咋不执行
  详细解决方案

crontab 定时施行shell脚本,脚本里的sql咋不执行

热度:792   发布时间:2016-04-24 07:16:21.0
crontab 定时执行shell脚本,脚本里的sql咋不执行?
shell 脚本如下:[oracle@abc sh]$ cat syn_wftable.sh 

#!/bin/sh

#----------data_output-------#
login_passwd=sccn/sccn  
TMPFILE=/home/oracle/sh/log/syn_wftable.log
date '+%D %T%n'
        sqlplus $login_passwd as sysdba <<EOF
        conn sccn/sccn
        set pagesize 0;
        set linesize 1000;
        set trims off;
        set echo off;
        set serveroutput on
        
        execute  SYNRO_TBL_WFINFO(sysdate-3,sysdate);

     spool ${TMPFILE}
     execute dbms_output.put_line('after execute SYNRO_TBL_WFINFO now time:'||to_char(sysdate,'YYYY-MM-DD HH24:MI:SS'));
    spool off
    
    exit 
  EOF

#


crontab 定时配置:
[oracle@abc ~]$ crontab -l
0-59 * * * *  /home/oracle/sh/sh/syn_wftable.sh >> /home/oracle/syn_wftable_log.txt
#0-59 * * * *  /home/oracle/sh/sh/synchronize_wfinfo.sh >> /home/oracle/wfinfo_log.txt

手动执行shell脚本正确执行了脚本里的存储过程,但定时自动执行,就是不执行sql相关的?而date '+%D %T%n' 执行了的,为什么?求高手解答





------最佳解决方案--------------------
你把crontab改下,加上2>&1,把stderr也输出到文件,
/home/oracle/sh/sh/syn_wftable.sh >> /home/oracle/syn_wftable_log.txt 2>&1
看看报什么错?
------其他解决方案--------------------
-- 在脚本前面引用一下当前用户的环境变量,
-- 例如,如果你是以oracle用户执行此脚本的话,在脚本中添加如下::
#!/bin/sh

source /home/oracle/.bash_profile
------其他解决方案--------------------
没看到你发了日志啊,按理说不管执不执行,都有sqlplus登录日志的
你把sqlplus $login_passwd as sysdba <<EOF
改成sqlplus /nolog <<EOF看看,我觉得sccn没有sysdba权限
还有,你最后一个EOF前面不要有空格

另外你需要检查下初始环境变量有没有ORACLE_SID
------其他解决方案--------------------
引用:
shell 脚本如下:[oracle@abc sh]$ cat syn_wftable.sh 

#!/bin/sh

#----------data_output-------#
login_passwd=sccn/sccn  
TMPFILE=/home/oracle/sh/log/syn_wftable.log
date '+%D %T%n'
        sqlplus ……


在crontab里的最前面写上:
ORACLE_HOME= 你的oracle。。。
ORACLE_BASE=。。。
ORACLE_SID=。。。

如果以上不行,再这样试试:
用root写crontab,其中运行命令这样写 
su - 用户名 -c 需要运行的脚本 


------其他解决方案--------------------
引用:
你把crontab改下,加上2>&amp;1,把stderr也输出到文件,
/home/oracle/sh/sh/syn_wftable.sh >> /home/oracle/syn_wftable_log.txt 2>&amp;1
看看报什么错?


谢谢!报错了,请问环境变量需要怎么设置啊?
11/13/12 13:57:01

/home/oracle/sh/sh/syn_wftable.sh: line 9: sqlplus: command not found