繁体   English   中英

使用 cron 时,一个 python 脚本执行,另一个不执行

[英]One python script executes, another does not, when using cron

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

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

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

后续.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 上搜索描述中带有回访的用户并关注他们。

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

有什么建议?

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

暂无
暂无

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

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