CRON and SQLPLUS

自闭症网瘾萝莉.ら 提交于 2019-12-20 04:31:26

问题


I want to run a script, which contains some sqlplus commands, in cron.

The problem is, that the sqlplus command won't be executed for some reason, when executed in cron. If I execute it by myself, the script runs fine.

I've checked some forums, even the topics here on stackoverflow.com and found some tips regarding the correct setting of environment variables. But even after double checking this, the script doesn't work.

Here is my script:

echo $ORACLE_HOME|grep "oracle" > /dev/null
if [ $? = 1 ] ; then
   echo "Setting environment variable"
   # Setting oracle environmet
   . /usr/oracle/product/10.2.0/.profile
   NLS_LANG='AMERICAN_GERMANY.WE8ISO8859P1'
fi

/usr/oracle/product/10.2.0/bin/sqlplus username/password @basics.sql > export.file

basics.sql contains:

set pagesize 0
set feedback off
set heading off
set linesize 400
set NULL nll

SELECT SOME_FIELDS FROM TABLE ORDER BY FIELD;
EXIT;

Any ideas?


回答1:


shell environment is very important for Oracle and almost not there when using cron. As always there are several ways to solve this.

  1. use full qualified paths - a bit inflexible
  2. make the script to setup it's own execution environment
  3. setup the execution environment in cron, when calling the script.

A pretty much standard way of setting up your environment from withing the script is by using the oraenv script, normally located in /usr/local/bin

ORACLE_SID={your_sid}
ORAENV_ASK=NO
type oraenv >/dev/null 2>&1 || PATH=/usr/local/bin:$PATH
. oraenv
SQLPATH=$HOME/sql
export SQLPATH
do your stuff

from the cron line:

10 10 * * * $HOME/.profile;$HOME/bin/your_script >$HOME/log/your_script.log 2>&1

This assumes that the .profile is not interactive and export the needed environment.



来源:https://stackoverflow.com/questions/17922688/cron-and-sqlplus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!