Python脚本在bash中运行,但不在cron中运行?

时间:2017-04-07 22:06:07

标签: python linux bash python-2.7 cron

我编写了一个python脚本转到reddit(使用PRAW),转到特定的subreddit,并使用youtube-dl从Youtube下载播客的剧集。我将脚本放在我的/ bin文件夹中,所以我可以从任何地方运行它是bash。问题是它在bash中运行良好,但根本不在cron中运行。我已经在cron中设置了这个:

0 */1 * * * PKA.py

这是cron放入/ var / mail:

的堆栈跟踪的副本
Traceback (most recent call last):
     File "/bin/PKA.py", line 48, in <module>
    call(myCall)
  File "/usr/lib/python2.7/subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

系统调用时发生崩溃。它清楚地给出了错误&#39;没有这样的文件或目录&#39;,但所有目录都是正确的。我只使用绝对路径,所以这不应该是一个问题。我很难搞清楚这个问题,我很感激你们给予的任何帮助。这是完整的脚本:

#!/usr/bin/env python
import praw
import os
from subprocess import call

#Directories
PKA_DIR = '/home/chris/Storage/750g/PKA/'
PKA_DOWNLOADED = '/home/chris/Storage/750g/PKA/pka-downloaded'
PKN_DIR = '/home/chris/Storage/750g/PKN/'
PKN_DOWNLOADED = '/home/chris/Storage/750g/PKN/pkn-downloaded'

#Create the reddit instance
reddit = praw.Reddit(Deleted for privacy)
subreddit = reddit.subreddit(Deleted Subreddit Name)

for submission in subreddit.new(limit=5):
    isDownloaded = False
    with open(PKN_DOWNLOADED, 'r') as downloaded:
        for line in downloaded:
            if submission.url in line:
                isDownloaded = True
            if line in submission.url:
                isDownloaded = True
        if isDownloaded == False:
            title, episode = submission.title.split()
            if title == 'PKN':
                myTitle = 'PKN #%s'%(episode) + '.%(ext)s'
                myCall = ['youtube-dl', '-f ' 'bestvideo[ext=mp4]+bestaudio[ext=m4a]', '-o' '%s%s'%(PKN_DIR,myTitle), submission.url]
                call(myCall)
                with open(PKN_DOWNLOADED, 'a') as writeOut:
                    writeOut.write(submission.url + '\n')
    isDownloaded = False
    with open(PKA_DOWNLOADED, 'r') as downloaded:
        for line in downloaded:
            if submission.url in line:
                isDownloaded = True
            if line in submission.url:
                isDownloaded = True
        if isDownloaded == False:
            title, episode = submission.title.split()
            if title == 'PKA':
                myTitle = 'PKA #%s - '%(episode) + '%(title)s.%(ext)s'
                myCall = ['youtube-dl', '-f ' 'bestvideo[ext=mp4]+bestaudio[ext=m4a]', '-o' '%s%s'%(PKA_DIR,myTitle), submission.url]
                call(myCall)
                with open(PKA_DOWNLOADED, 'a') as writeOut:
                    writeOut.write(submission.url + '\n')

程序登录机器人帐户(我知道这不是必需的,我只是想尝试PRAW的全部功能),然后导航到subreddit。它考虑了最近的5个帖子,并从中拉出了youtube链接。它根据先前下载的剧集的文本文件数据库检查链接。如果尚未下载该剧集,则会下载该剧集并将其添加到列表中。它将所有必要的参数放入列表中,并使用call进行系统调用。我怀疑这个问题是由于权限,但我的搜索告诉我,cron以root身份运行作业。它下载到的文件夹(`/ home / chris / Storage / 750g&#39;)都在单独的驱动器上,这就是为什么我认为权限可能是个问题。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

最简单的解决方案是使用/usr/local/bin/youtube-dl的完整绝对路径。在您的情况下,请使用youtube-dl代替PATH

原因:

某些Linux发行版(如Ubuntu)在用户访问shell时将原始/usr/local/bin变量更改为包含PATH中的命令。但是,cron将一组最小的环境变量传递给作业,并保留原始 select cast(cast(getdate() as date) as datetime) [1]