Python无法在cron中找到共享库 [英] Python cannot find shared library in cron

查看:446
本文介绍了Python无法在cron中找到共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Python脚本在shell中运行良好。但是,当我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: :libpython2.7.so.1.0:无法打开共享对象文件:无此类文件或目录

/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

我知道我的〜/ .bashrc文件中有以下行,这解释它在shell中有效

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

它使用以下它也工作,但它看起来很丑陋,我讨厌应用于每个cron工作。

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 

有更好的方法吗?我知道我们的管理员以前有一个早期版本的Python安装在一些共享的nfs路径,它不需要任何系统级配置更改如上所述这里。我们的旧Python脚本只有这一行作为第一行没有显式设置LD_LIBRARY_PATH。

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

在旧nfs安装

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

当前Python是2.7.3,它安装如下:(Linux CentOS 6)

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

更新: p>

Update:


  1. 正如ansh0I建议的,在cronab的顶部添加LD_LIBRARY_PATH!

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

python抱怨共享库的原因是它是用 - enable-shared 安装的。因此,python二进制文件要小得多,在/usr/local/lib/libpython2.7.so中有很多真正的解释器代码 shared 。然后你需要告诉python在哪里找到共享库,通过设置LD_LIBRARY_PATH。如果python安装没有--enable-shared ,二进制文件本身大得多,你不需要指定任何LD_LIBRARY_PATH

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


推荐答案

假设您的 LD_LIBRARY_PATH = $ LD_LIBRARY_PATH:/ usr / local / lib 工作正常,您可以在crontab文件顶部设置环境变量,如下所示

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

这篇关于Python无法在cron中找到共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆