问题
I am using cx_Oracle library in my python script. My code works fine if I directly execute by python script from Linux terminal but when I put it in crontab I am getting following error.
!!DatabaseError: DPI-1047: Oracle Client library cannot be loaded: libclntsh.so: cannot open shared object file: No such file or directory. See https://oracle.github.io/odpi/doc/installation.html for help
DPI-1005: unable to acquire Oracle environment handle !!Traceback (most recent call last): import cx_Oracle !!DatabaseError: DPI-1005: unable to acquire Oracle environment handle
I googled this issue and It seems some environment variable missing when it ran using crontab. I try to export following in crontab but it does not work.
export LD_LIBRARY_PATH='/usr/lib/oracle/11.2/client64/lib'
If I remove cx_Oracle package other code runs fine. I have only one version of python installed on my machine.
What is your version of Python? Is it 32-bit or 64-bit? Python 2.6 . 64 bit
What is your version of cx_Oracle? Version 6.0b1
What is your version of the Oracle client (e.g. Instant Client)? How was it installed? Where is it installed? oracle-instantclient11.2-devel-11.2.0.4.0-1.x86_64.rpm
What is your OS and version? CentOS 6.7
What environment variables did you set? How exactly did you set them? export LD_LIBRARY_PATH='/usr/lib/oracle/11.2/client64/lib'
回答1:
Evidently cron doesn't load the bash profile so you will need write a wrapper.
So, write a bash wrapper that exports the variables needed and calls the script. Then call that wrapper from crontab. When that works, you will know that your variables weren't being properly exported for the user whose crontab you edited.
Note: You can add them into /etc/bashrc
and it will then be in place for all users, if you have root access.
You can also make a generic wrapper and cron things be sending them through the wrapper.
my_bash_wrapper.sh
#!/bin/bash
. ~/.bash_profile
"$0"
evoke in cron:
0 1 * * * /my/loc/my_bash_wrapper.sh my_python_script arg1 arg2
回答2:
You should set ORACLE_HOME and LD_LIBRARY_PATH in your bash script.
#!/bin/bash
export ORACLE_HOME=/usr/lib/oracle/<version>/client(64)
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
来源:https://stackoverflow.com/questions/43794503/cx-oracle-package-not-working-inside-crontab