6
votes

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

1
thanks for the update :) the --enable-shared just saved melittle-dude

1 Answers

8
votes

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