Nick Lunsford
Nick Lunsford

Reputation: 13

How can I automate Python program on Raspberry Pi with cron?

I'm building a basic Twitter scraper with Python that I want to run off of my RaspPi 4b on an hourly basis. The script is written and works perfectly when called from the terminal using

python scraper.py

Now, I want to automate it to run without my own physical prompt. I did chmod with the script, then opened the crontab, and using the editor added this line (I understand that it's for every minute, I just want to see it work):

* * * * * /usr/bin/python home/pi/Desktop/twitter_scraper/scraper.py

However, nothing executes on its own. I'm not quite sure why this is because I specified the directories of both the Python program and the interpreter. Do I need to add anything else besides that line to the cron file? The Python script does access other files located in the same directory, but I did not think that would matter much. Do I need to restart my Pi for it to take effect?

Upvotes: 1

Views: 334

Answers (2)

Romeo Ninov
Romeo Ninov

Reputation: 7225

When it come to python is better to run the code in to the directory where it is located. And in such case the cron will be something like:

* * * * * cd /home/pi/Desktop/twitter_scraper; /usr/bin/python scraper.py

Upvotes: 1

Moeen Tabrizi
Moeen Tabrizi

Reputation: 44

you do not need anything more just do in this way

* * * * * python /home/pi/Desktop/twitter_scraper/scraper.py

if it doesn't work you can check your system log. and see the errors for debugging.

Upvotes: 0

Related Questions