【问题标题】:Connection to Oracle db (cx_Oracle) not working when scheduled from crontab but works as standalone从 crontab 计划时与 Oracle db (cx_Oracle) 的连接不工作,但作为独立工作
【发布时间】:2019-01-30 19:27:09
【问题描述】:

我有一个 python (3.6) 脚本,它对从 Oracle DB 检索到的数据执行一些操作。 然后我有一个用于执行脚本的自动化 shell 脚本。我会在 crontab 中安排此类 shell 的执行。

如果我将 shell 作为独立 (sh shell.sh) 执行,它工作得非常好,而当它作为 crontab 作业运行时,与 Oracle DB 的连接失败 --> class 'cx_Oracle.DatabaseError'

我正在尝试简化我正在使用的代码,Python 脚本如下:

#!/usr/bin/python3.6
# coding: utf-8
import pandas as pd
import cx_Oracle
try:
    conn = cx_Oracle.connect(username/pwd@hostname:port/db_name)
    query = """SELECT * FROM schema.table"""
    df = pd.read_sql(query, con = conn)
except:
    print(sys.exc_info()[0])
    raise
    sys.exit(1)

shell 正在做这样的事情:

#!/usr/bin/env bash
if [something]
then
    python3.6 pyscript.py
fi

crontab -l 结果:

* * * * * cd /root/workindirectory ; sudo sh shell.sh >> test.out

在此先感谢您的任何帮助。

编辑:指定 shell 和 python 脚本都有 777 权限

【问题讨论】:

    标签: python python-3.x shell cron cx-oracle


    【解决方案1】:

    很抱歉,我认为这是 cx_Oracle Package Not working inside Crontab 的副本

    由于 cron 不加载 bash 配置文件,我找到了在 shell 脚本中导出 ORACLE_HOME 和 LD_LIBRARY_PATH 的解决方案。正如链接问题中所指出的,要添加的行是:

    export ORACLE_HOME=/usr/lib/oracle/<version>/client(64)
    export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
    

    【讨论】:

      猜你喜欢
      • 2015-10-25
      • 2015-07-14
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-27
      相关资源
      最近更新 更多