简体   繁体   English

Python在cron中找不到共享库

[英]Python cannot find shared library in cron

My Python script runs well in the shell. 我的Python脚本在shell中运行良好。 However when I cron it (under my own account) it gives me the following error: 然而,当我cron它(在我自己的帐户下)它给我以下错误:

/usr/local/bin/python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory / usr / local / bin / python:加载共享库时出错:libpython2.7.so.1.0:无法打开共享对象文件:没有这样的文件或目录

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 我知道我的〜/ .bashrc文件中有以下行,这解释了它在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. 如果我使用以下内容进行cron它也可以工作,但它看起来很难看,而且我讨厌申请每个cron工作。

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 . 我知道曾经有过的Python安装一些共享NFS路径上的早期版本我们的管理,并且提到不需要任何系统级别的配置变化在这里 Our old Python script simply has this line as the first line no explicit setting the LD_LIBRARY_PATH. 我们的旧Python脚本只是将这一行作为第一行没有显式设置LD_LIBRARY_PATH。

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

In the old nfs installation 在旧的nfs安装中

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

Current Python is 2.7.3, and it is installed as follows: (Linux CentOS 6) 当前的Python是2.7.3,安装如下:(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! 正如ansh0I建议的那样,将LD_LIBRARY_PATH添加到cronab的顶部可以正常工作!

  2. The reason python complained about the shared libraries is it is installed with --enable-shared . python抱怨共享库的原因是它与--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. 因此,python二进制文件要小得多,在/usr/local/lib/libpython2.7.so中共享了许多真正的解释器代码。 Then you need to tell python where to find the shared library by setting LD_LIBRARY_PATH. 然后你需要通过设置LD_LIBRARY_PATH告诉python在哪里找到共享库。 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 如果在没有--enable-shared的情况下安装了python,那么二进制文件本身要大得多,并且您不需要指定任何LD_LIBRARY_PATH

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 假设您的LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib表达式工作正常,您可以在crontab文件的顶部设置环境变量,如下所示

#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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM