oracle CRON 和 SQLPLUS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17922688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:49:35  来源:igfitidea点击:

CRON and SQLPLUS

oracleunixcronsqlplus

提问by Marco Frost

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

我想在 cron 中运行一个包含一些 sqlplus 命令的脚本。

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.

问题是,当在 cron 中执行时,sqlplus 命令由于某种原因不会被执行。如果我自己执行它,脚本运行良好。

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.

我查看了一些论坛,甚至是 stackoverflow.com 上的主题,并找到了一些有关正确设置环境变量的提示。但即使经过仔细检查,脚本也不起作用。

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:

basics.sql 包含:

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?

有任何想法吗?

回答by ik_zelf

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

shell 环境对 Oracle 非常重要,而在使用 cron 时几乎不存在。与往常一样,有多种方法可以解决此问题。

  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.
  1. 使用完全限定的路径 - 有点不灵活
  2. 使脚本设置它自己的执行环境
  3. 调用脚本时,在 cron 中设置执行环境。

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

使用脚本设置环境的一种非常标准的方法是使用 oraenv 脚本,通常位于 /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:

从 cron 行:

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.

这假设 .profile 不是交互式的并导出所需的环境。