我有一个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上搜索描述中具有后续内容的用户并跟随它们。
test.py脚本在cron中运行良好,但是followback.py没有,我不知道可能出现什么问题。
有什么建议吗?