【问题标题】:cx_Oracle Package Not working inside Crontabcx_Oracle 包在 Crontab 中不起作用
【发布时间】:2017-10-03 07:39:33
【问题描述】:

我在我的 python 脚本中使用 cx_Oracle 库。如果我直接从 Linux 终端通过 python 脚本执行,我的代码可以正常工作,但是当我把它放在 crontab 中时,我得到了以下错误。

!!DatabaseError: DPI-1047: Oracle Client library cannot be loaded: libclntsh.so: cannot open shared object file: 没有这样的文件或目录。请参阅https://oracle.github.io/odpi/doc/installation.html 寻求帮助

DPI-1005:无法获取 Oracle 环境句柄 !!回溯(最近一次通话最后): 导入 cx_Oracle !!DatabaseError: DPI-1005: 无法获取 Oracle 环境句柄

我用谷歌搜索了这个问题,使用 crontab 运行时似乎缺少一些环境变量。 我尝试在 crontab 中导出以下内容,但它不起作用。

导出 LD_LIBRARY_PATH='/usr/lib/oracle/11.2/client64/lib'

如果我删除 cx_Oracle 包,其他代码运行良好。我的机器上只安装了一个版本的 python。

您的 Python 版本是多少?它是 32 位还是 64 位? 蟒蛇 2.6 。 64位

您的 cx_Oracle 版本是多少? 版本 6.0b1

您的 Oracle 客户端版本是什么(例如 Instant Client)?它曾是怎样的 安装?它安装在哪里? oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm

您的操作系统和版本是什么? CentOS 6.7

你设置了什么环境变量?你究竟是如何设置它们的? export LD_LIBRARY_PATH='/usr/lib/oracle/11.2/client64/lib'

【问题讨论】:

    标签: oracle environment-variables crontab cx-oracle


    【解决方案1】:

    显然 cron 不会加载 bash 配置文件,因此您需要编写一个包装器。

    所以,编写一个 bash 包装器来导出所需的变量并调用脚本。然后从 crontab 调用该包装器。当它起作用时,您将知道您的变量没有为您编辑的 crontab 的用户正确导出。

    注意:您可以将它们添加到 /etc/bashrc,如果您有 root 访问权限,所有用户都可以使用它。

    您还可以制作一个通用包装器,并通过包装器发送它们。

    my_bash_wrapper.sh

    #!/bin/bash
    
    . ~/.bash_profile
    "$0"
    

    在 cron 中调用:

    0 1 * * * /my/loc/my_bash_wrapper.sh my_python_script arg1 arg2
    

    【讨论】:

      【解决方案2】:

      您应该在 bash 脚本中设置 ORACLE_HOME 和 LD_LIBRARY_PATH。

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

      【讨论】:

        【解决方案3】:

        由于它正在寻找 oracle 即时客户端路径,它无法正常工作。 在 shell 脚本中,添加导出行并将其指向您的 oracle 客户端路径。接下来,继续编写你的 shell 脚本。这会奏效!

        #!/bin/sh
        export LD_LIBRARY_PATH=/home/<user_name>/opt/oracle/instantclient_19_5:$LD_LIBRARY_PATH
        /usr/anaconda3/bin/python /home/<user_name>/test/src/test.py
        

        【讨论】:

          【解决方案4】:

          它可能适用于某些用户 我正在使用 Gnome Schedular 来安排 python scipts。

          对于普通工作。

          ~anaconda3/bin/python /path_to_script/target_script.py

          对于需要环境变量的作业。

          。 ~/bash_profile ~anaconda3/bin/python /path_to_script/target_script.py

          【讨论】:

            猜你喜欢
            • 2011-12-19
            • 2013-11-12
            • 2018-12-21
            • 2011-07-23
            • 2015-01-23
            • 2017-04-14
            • 2020-04-21
            • 2017-12-24
            相关资源
            最近更新 更多