【问题标题】:One python script executes, another does not, when using cron使用 cron 时,一个 python 脚本执行,另一个不执行
【发布时间】:2020-06-08 14:31:07
【问题描述】:

我有一个 python 脚本,followback.py,我正在尝试使用 cron 来运行它。 该脚本本身运行良好,即当通过命令“python followback.py”运行时。 但是使用 cron 时该脚本永远不会运行。 我的 crontab 文件:

* * * * * python /home/ubuntu/./followback.py
* * * * * python /home/ubuntu/./test.py

我使用 test.py 作为一个简单的测试措施,通过写入一个文件让我知道它已经运行。

followback.py:

import io, json

def save_json(filename, data):
    with io.open('{0}.json'.format(filename), 
                 'w', encoding='utf-8') as f:
        f.write(unicode(json.dumps(data, ensure_ascii=False)))

def load_json(filename):
    with io.open('{0}.json'.format(filename), 
                 encoding='utf-8') as f:
        return f.read()

CONSUMER_KEY = xx
CONSUMER_SECRET = xx
OAUTH_TOKEN = xx
OAUTH_TOKEN_SECRET = xx

auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
                           CONSUMER_KEY, CONSUMER_SECRET)

twitter_api = twitter.Twitter(auth=auth)

q = 'followback'
count = 20
page = 1
results = []
maxResults = 50
filename = 'attempted_accounts'
try:
    usedUsers = json.loads(load_json(filename))
except IOError:
    usedUsers = []
usedList = [used['id'] for used in usedUsers]

# search for 'followback' and follow the ones with 'followback' in description
while len(results) < maxResults:
    users = twitter_api.users.search(q=q, count=count, page=page)
    results += [user for user in users if 'followback' in user['description'] and user['id'] not in usedList]
    page += 1

[twitter_api.friendships.create(user_id=user['id'], follow='true') for user in results]
out = usedUsers + [{'id' : e['id']} for e in results]
save_json(filename, out)

上面的脚本只是简单地在 twitter 上搜索描述中带有 followback 的用户并关注他们。

test.py 脚本通过 cron 运行良好,但 followback.py 没有,我不知道可能出了什么问题。

有什么建议吗?

【问题讨论】:

    标签: python cron


    【解决方案1】:

    检查followback.py 文件是否可执行,如果不使用chmod +x,它应该可以工作。这是最常见的问题。看类似案例here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多