简体   繁体   中英

One python script executes, another does not, when using cron

I have a python script, followback.py, which I am trying to run by using cron. The script runs fine on its own ie when run by command 'python followback.py'. But the script is never run when using cron. My crontab file:

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

I am using test.py as a simple testing measure by writing to a file to let me know that it have been run.

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)

The script above simply searches twitter for users with followback in the description and follows them.

The test.py script runs fine through cron but followback.py does not and I have no clue as to what could be wrong.

Any suggestions?

Check if the followback.py file ex executable, if not use chmod +x and it should work. That's the most common issue with this. Look at similar case here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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