从 crontab 计划时与 Oracle 数据库 (cx_Oracle) 的连接不工作,但作为独立工作

Connection to Oracle db (cx_Oracle) not working when scheduled from crontab but works as standalone

我有一个 python (3.6) 脚本,它对从 Oracle 数据库检索到的数据执行一些操作。 然后我有一个 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 权限

抱歉,我认为这是 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