Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python cannot find shared library in cron

My Python script runs well in the shell. However when I cron it (under my own account) it gives me the following error:

/usr/local/bin/python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

The first line of the script has:

#!/usr/local/bin/python

I know I have the following line in my ~/.bashrc file, which explains it works in the shell

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

If I cron it using the following it also works, but it looks ugly, and I hate to apply to every cron job.

00 * * * 1-5    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib && /path/somejob.py 

Is there a better way to do it? I know our admin used to have an earlier version of Python installed on some shared nfs path, and it does not require any system level config change as mentioned here. Our old Python script simply has this line as the first line no explicit setting the LD_LIBRARY_PATH.

#!/nfs/apps/python/bin/python

In the old nfs installation

/nfs/apps/python/
  -- bin
  -- lib
  -- share
  -- include

Current Python is 2.7.3, and it is installed as follows: (Linux CentOS 6)

./configure --prefix=/usr/local --enable-shared --with-system-expat --with-system-ffi
make
make install

Update:

  1. As ansh0I suggested, adding LD_LIBRARY_PATH to the top of cronab works!

  2. The reason python complained about the shared libraries is it is installed with --enable-shared. As a result the python binary file is much smaller, with much of the real interpreter code shared in /usr/local/lib/libpython2.7.so. Then you need to tell python where to find the shared library by setting LD_LIBRARY_PATH. If python is installed without --enable-shared, the binary file itself is much larger, and you don't need to specify any LD_LIBRARY_PATH

like image 438
fivelements Avatar asked Oct 18 '13 15:10

fivelements


People also ask

Where does Python Look for shared libraries?

We can see that the system python only looks for its shared libraries in the ~ directory, some specified in the PYTHONPATH and some directories in /usr/lib and /usr/local/lib (mainly /usr/lib ), and only looks for the dist-packages instead of site-packages.

Where are the Cronjobs stored?

Cron jobs are stored in a crontab file by username. These files are stored in /var/spool/cron/crontabs or /var/spool/cron/ . These files should not be edited directly. The crontab command should always be used to make changes.

Can I run Python script from cron?

The simplest way to do automation with Python is by using crontab (cron) on Mac or Task Scheduler on Windows. In this guide, you will learn how to use crontab run your Python scripts automatically. To know more, follow the rest of the tutorial below.


1 Answers

Assuming your LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib expression is working fine, you can set up environment variables at the top of the crontab file like below

#Setting up Environment variables
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

#Here follow the cron jobs
* * * * *   echo $LD_LIBRARY_PATH >> /home/user/logfile.log
* * * * *   some/cron/job.py
like image 123
Anshul Goyal Avatar answered Sep 19 '22 00:09

Anshul Goyal