【问题标题】:Python script run via cron job returning IOError [error no2]Python 脚本通过 cron 作业运行返回 IOError [error no2]
【发布时间】:2015-10-15 20:01:00
【问题描述】:

我正在通过 Centos6 远程服务器上的 cron 作业运行 Python feedparser 脚本(通过 SSH 连接到服务器)。

在 Crontab 中,这是我的 cron 工作:

MAILTO = myemail@company.com
*/10 * * * * /home/local/COMPANY/malvin/SilverChalice_CampusInsiders/SilverChalice_CampusInsiders.py > /home/local/COMPANY/malvin/SilverChalice_CampusInsiders`date +\%Y-\%m-\%d-\%H:\%M:\%S`-cron.log | mailx -s "Feedparser Output" myemail@company.com

但是,我在发送的电子邮件中看到了这条消息,它应该只包含脚本的输出:

Null message body; hope that's ok
/usr/lib/python2.7/site-packages/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
Traceback (most recent call last):
  File "/home/local/COMPANY/malvin/SilverChalice_CampusInsiders/SilverChalice_CampusInsiders.py", line 70, in <module>
    BC_01.createAndIngest(name, vUrl, tags, desc)
  File "/home/local/COMPANY/malvin/SilverChalice_CampusInsiders/BC_01.py", line 69, in createAndIngest
    creds = loadSecret()
  File "/home/local/COMPANY/malvin/SilverChalice_CampusInsiders/BC_01.py", line 17, in loadSecret
    credsFile=open('brightcove_oauth.json')
IOError: [Errno 2] No such file or directory: 'brightcove_oauth.json'

通常,这将是一个不费吹灰之力的问题:我的代码一定有问题。除了,当我通过python SilverChalice_CampusInsiders.py在命令行上运行该脚本时,该脚本运行良好

我在这里做错了什么?为什么 Python 脚本在通过 cron 作业运行时看不到 json oauth 文件?

【问题讨论】:

    标签: python python-2.7 cron centos6


    【解决方案1】:

    Cron 为作业设置了一个最小环境(我认为它从主目录运行作业)。

    在您的 python 脚本中,当您执行类似 -

    的操作时
    open('<filename>')
    

    它检查当前工作目录中的filename,而不是脚本所在的目录。

    即使从命令行运行也是如此,如果您将目录更改为其他目录(可能是您的主目录),然后使用脚本的绝对路径来运行它,您应该会收到相同的错误。

    您可以尝试以下任一选项,而不是依赖当前工作目录是否正确并拥有要打开的文件 -

    1. 使用要打开的文件的绝对路径,不要使用相对路径。

    2. 或者如果上述方法不适合您,并且您要打开的文件相对于正在运行的脚本存在(例如,目的可以说在同一目录中),那么您可以使用__file__ (这给出了脚本位置)和 os.path ,在运行时创建文件的绝对路径,示例 -

      import os.path
      
      fdir = os.path.abspath(os.path.dirname(__file__)) #This would give the absolute path to the directory in which your script exists.
      f = os.path.join(fdir,'<yourfile')
      

    最后f 将拥有您文件的路径,您可以使用它来打开您的文件。

    【讨论】:

    • 感谢您的详细回复,非常感谢。因此,如果我的所有三个文件都在同一个目录中(2 个 py 脚本和一个 json oauth),那么这两行(我假设我应该放在我的主 py 脚本中)看起来像这样吗? fdir = os.path.abspath(/home/local/MYCOMPANY/malvin/SilverChalice_ CampusInsiders)
    • 我只需要从另一个 py 文件中导入 oauth.json 文件和一些函数,我现在是否需要重组我的主 py 脚本,以便他们使用 f = os.path.join(fdir,'&lt;yourfile') 打开这两个其他文件?
    • 您可以使用 __file__ ,如果您不使用它,那么在打开文件时使用完整(绝对路径)会更容易。这仅用于打开文件,导入应该可以正常工作(导入其他 .py 文件)。
    • 如果我需要我的主 py 脚本访问的另一个文件是 oauth.json 我应该使用 f = os.path.join(fdir,'oauth.json') 吗?现在在我的主脚本中,我有credsFile=open('brightcove_oauth.json'),并且我的电子邮件中不断收到错误消息,上面写着IOError: [Errno 2] No such file or directory: 'oauth.json'——我应该将credsFile 行替换为f = os.path..... 行吗?
    • 如果 fdir 如上所示(但带有字符串/引号),则可以
    猜你喜欢
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 2011-03-27
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多