Crontab not running python script Tried multiple fixes New to terminal go easy on me [closed]
Hi all and thank you for reading i'll discuss underneath.
I have a simple python script that prints a random fact from reddit into the terminal i'm testing the waters of terminal whilst learning python.
I've tried changing my permissions to access the /usr/bin/ dir. No success
I've generated a new reddit.py script inside the /usr/bin dir as i read elsewhere that crontab only runs from its home directory and if both pathways are given in the crontab file this should solve the problem of it running. Again no success.
I've given myself full read, write and execute permissions on / apart from 'proc' & 'sys' which were denied.
It will run if i write the pathway in terminal and prints a random fact from the TIL from reddit.
Will not run through crontab any further information anyone can provide will be greatly received and if you can please provide any simplified detail as i've just started with this and i'm getting the hang of things slowly but surely. Again many thanks. I'll provide a copy of script below.
I am running Debian 10 on a AMD 64 chromebook.
#! /usr/bin/env python
import requests
import json
subreddit = 'todayilearned'
count = 1
timeframe = 'day' #hour, day, week, month, year, all
listing = 'random' # controversial, best, hot, new, random, rising, top
def get_reddit(subreddit,count):
try:
base_url = f'https://www.reddit.com/r/{subreddit}/{listing}.json?count={count}&t={timeframe}'
request = requests.get(base_url, headers = {'User-agent': 'yourbot'})
except:
print('An Error Occured')
return request.json()
top_post = get_reddit(subreddit,count)
if listing != 'random':
title = top_post['data']['children'][0]['data']['title']
url = top_post['data']['children'][0]['data']['url']
else:
title = top_post[0]['data']['children'][0]['data']['title']
url = top_post[0]['data']['children'][0]['data']['url']
print(f'{title}\n{url}')
Crontab file:
*/5 * * * * /usr/bin/python /usr/bin/reddit.py >dev/null 2>&1
Thanks, I've been trying to resolve this for a good 12 hours and still no success. It is helping me become comfortable with the environment and commands though! So silver linings =)
It is an extremely bad idea to change permissions on / or /usr/bin or place files in those directories. If you are going to install a local program like this at the system level, it should go in /usr/local/bin
It is also unnecessary to do this to accomplish what you are trying, it doesn't have to be in a system directory to run from cron, especially a user's cron. You should be able to place this file in your home directory and just give the full path to it as you are already doing in the crontab entry.
Normally cron mails you errors when things fail. However, you may not have mail installed, and you are disabling any error logging anyway with >dev/null 2>&1
Most likely if you fix these things and capture the error that is preventing your script from running, you'll find out what the real problem is. (If you can't figure it out from the errors once you get them, add them to your question.)