I have Python script which inserts certain data to an Oracle Autonomous Database. I order to connect to the Oracle Database, I am using cx_Oracle module. When I run this script directly, there is no problem. However, when I tried to run a crontab it threw this error:
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: “libclntsh.so: cannot open shared object file: No such file or directory”
A similar question is asked here. The accepted answer did not work for me but one of the comments in one of the answers helped me to solve this problem.
For this just use
sudo sh -c "echo /opt/oracle/instantclient_21_4 > /etc/ld.so.conf.d/oracle-instantclient.conf" sudo ldconfig
2. Set the environment variables in a shell script and then invoking Python from that script:
#create a file to contain shell script: nano ourshellscript.sh #place the following code to the top of the file: #! /bin/sh #Set the environment variables and write your Python code here and save this file.
#! /bin/sh export PATH="$PATH:/opt/oracle/instantclient_21_4" export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/opt/oracle/instantclient_21_4" sudo /usr/bin/python3 /root/binance_hist/myPythonScript.py
3. Set the crontab:
0 * * * * bash /root/binance_hist/ourshellscript.sh >> /root/binance_hist/hourly_alert.log 2>&1
So, instead of calling the Python script directly from crontab, we create a shell script, call the Python from shell script along with necessary environment variables and then set a cronjob so that it runs the shell script. That solves the problem.