Search code examples
pythonrhel

importing paramiko and mysql.connector packages not working when ran as cron job


Importing packages that are subsequently installed (not present by default in python distribution for rhel 7.6) not working when ran as cron job

Hi Team,

I have a python(2.7) script which imports paramiko package. The script can import the paramiko package successfully when ran as a user(root or ftpuser) after logging in but it cannot import it when ran from cron job. I have tried out various options as provided in the brilliant stack overflow pages like the below but unfortunately couldn't resolve the issue. 1) Crontab not running my python script

I have provided the path to the paramiko package and verified it is successfully received at the script end by logging it when run as cron job and also I have given chmod -R 777 permission to the paramiko folder in the /opt/rh/python27/root/usr/lib/python2.7/site-packages location. Still the import is not functioning when ran as cron job

I have created a shell script and tried to invoke python script from with in the script and configured the shell script in cron job but it seems python script was not invoked

I have verified that there is only one python installation present in the server and so I' am using the correct path

I have disabled selinux option and tried after rebooting but issue still persists

Please note the issue exists not for just paramiko package but for other packages as well that was installed subsequently like mysql.connector e t c

Update1

It has to be something to do with the way I install the paramiko package because the script can even import other packages in the same path as that of paramiko and the permissions for both of them look identical only difference is former comes with the python distribution that is deployed from using the url https://access.redhat.com/solutions/1519803. Cannot figure out what is wrong with the installation steps as I install it as root after doing sudo su and then setting umask to 0022. I do pip install of parmiko and python-crontab as mentioned in their sites and both have the same issue

Another interesting thing is though I have the code to log exception around the failing import statement it never logs the exception but script seems to halt/hang at the import statment

Please help to resolve this issue...

PYTHON CODE

#!/usr/bin/env python  
import sys  
import logging  
import os

def InitLog():  
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s %(levelname)s %(message)s',
        filename=os.path.dirname(os.path.abspath(__file__)) + '/test_paramiko.log',
        filemode='a'
        )
    logging.info('***************start logging****************')


InitLog()  
logging.info('before import')  
logging.info(sys.path)  
try:  
    sys.path.append("/opt/rh/python27/root/usr/lib/python2.7/site-packages") 

    logging.info("sys path appended before import")  
    import paramiko  
except ImportError:  
    logging.ERROR("Exception occured druing import")  

logging.info('after import')

CRONTAB Entry

SHELL=/bin/bash
PATH=/opt/rh/python27/root/usr/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ftpuser/.local/bin:/home/ftpuser/bin
PYTHONPATH=/opt/rh/python27/root/usr/lib64/python27.zip:/opt/rh/python27/root/usr/lib64/python2.7:/opt/rh/python27/root/usr/lib64/python2.7/plat-linux2:/opt/rh/python27/root/usr/lib64/python2.7/lib-tk:/opt/rh/python27/root/usr/lib64/python2.7/lib-old:/opt/rh/python27/root/usr/lib64/python2.7/lib-dynload:/opt/rh/python27/root/usr/lib64/python2.7/site-packages:/opt/rh/python27/root/usr/lib/python2.7/site-packages


*/1 * * * * /opt/rh/python27/root/usr/bin/python /home/ftpuser/Ganesh/test_paramiko.py

#*/1 * * * * /home/ftpuser/Ganesh/test_cron.sh >> /home/ftpuser/Ganesh/tes_cron.txt 2>&1

#*/1 * * * * /home/ftpuser/Ganesh/test_cron.sh

Shell script

#!/opt/rh/python27/root/usr/bin/python

export PATH=$PATH:/opt/rh/python27/root/usr/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ftpuser/.local/bin:/home/ftpuser/bin

export PYTHONPATH=$PYTHONPATH:/opt/rh/python27/root/usr/lib64/python27.zip:/opt/rh/python27/root/usr/lib64/python2.7:/opt/rh/python27/root/usr/lib64/python2.7/plat-linux2:/opt/rh/python27/root/usr/lib64/python2.7/lib-tk:/opt/rh/python27/root/usr/lib64/python2.7/lib-old:/opt/rh/python27/root/usr/lib64/python2.7/lib-dynload:/opt/rh/python27/root/usr/lib64/python2.7/site-packages:/opt/rh/python27/root/usr/lib/python2.7/site-packages

python /home/ftpuser/Ganesh/test_paramiko.py

Expected result from my python script is to log the "after import" string

But currently it is printing only till "sys path appended before import" which also shows the normal python packages are getting imported successfully


Solution

  • This seems to be working now after adding one more environment variable to crontab as below

    LD_LIBRARY_PATH=/opt/rh/python27/root/usr/lib64