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:
As ansh0I suggested, adding LD_LIBRARY_PATH to the top of cronab works!
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
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With