1

I've gone through multiple threads, but I still can't seem to find my problem.

I'm building a really simple Twitter bot that I'd like to fire every hour, on the hour with a cron job from a Raspberry Pi. Here's my crontab:

PYTHONPATH=/usr/bin/python
MAILTO=*myemail*
00 * * * * /home/username/directory/my_script.py >> /var/log/cron.log

Then the script:

#! /usr/bin/env python
import sys
from twython import Twython, TwythonError
from pymarkovchain import MarkovChain

#TWITTER ACCESS
apiKey = KEY
apiSecret = SECRET
accessToken = TOKEN
accessKey = KEY

#text to pull
text = open('/home/username/directory/text.txt').read()

#Generate database and frequency table
mc = MarkovChain('/home/username/directory/markov')
mc.generateDatabase(text)
tweet = mc.generateString()

api = Twython(apiKey,apiSecret,accessToken,accessKey)

try:
    api.update_status(status=tweet)
except TwythonError as e:
    print e

The first thing I checked was all of my referenced files to make sure they were absolute references. Then, I checked to make sure my file paths were correct. I'm really stumped here. Running the script from command line with the full path works as expected. Any thoughts are appreciated.

5
  • First of all, your shebang is broken. Second, that do you mean by "fail" / "doesn't work"? Commented Jul 10, 2014 at 20:45
  • Oh, that was a typo. I double checked my script, and it's fine there. And yes, the cron job isn't posting the tweet like it should. The log isn't showing anything wrong, either, from what I can tell. Commented Jul 10, 2014 at 20:48
  • PYTHONPATH should definitely not be /usr/bin/python, although I doubt this is your problem. Commented Jul 10, 2014 at 20:50
  • Does /var/log/cron.log exist at all? Is the cron job running as a user who has permission to write that file? Commented Jul 10, 2014 at 20:52
  • Well, you're currently only redirecting STDOUT to the log - I wouldn't expect any tracebacks to be in there. Try my_script.py &>> /var/log/cron.log to redirect STDERR to the log as well. Commented Jul 10, 2014 at 20:53

2 Answers 2

2

After trying the suggestions above and reading countless articles, I learned that cron has to run as root, not as the user. I checked the logs and saw that the user calling the script was the owner of the file, not root. So, running chmod a+x my_script.py took care of it.

Thanks for all the suggestions - especially those getting the errors to the correct log file.

Sign up to request clarification or add additional context in comments.

Comments

0

To debug better, you might want to redirect stderr:

00 * * * * /home/username/directory/my_script.py >> /tmp/cron.log 2>&1
# or
00 * * * * /home/username/directory/my_script.py >> /tmp/cron.log 2>/tmp/cron-error.log

(I also changed the path there to make sure your cron user has permission to write output.)

Another thing you could try is run the script with Python in cron:

00 * * * * python /home/username/directory/my_script.py >> /tmp/cron.log 2>&1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.