Score:0

Crontab not running python script Tried multiple fixes New to terminal go easy on me

ve flag

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 =)

codlord avatar
ru flag
It should be `/dev/null` if you want to redirect output there. But why not redirect to a log file and then the log file will contain messages/errors from your python command/script. If you don't get any meaningful messages/errors then add them to your script so you can see what it's doing and where it's going wrong.
hr flag
*"prints a random fact from reddit into the terminal"* cron doesn't know about any terminals that may be open, so any output will either be mailed to the crontab owner or discarded.
Score:1
in flag

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.)

Mentalist avatar
ve flag
Thank you for your help this is very informative, I wasn't fully aware that the >dev/null 2>&1 disabled error logging I found it as part of a fix for the crontab issue it was part of the #!/usr/bin/env python addition.
Mentalist avatar
ve flag
Sorry to sound like a complete noob, error logs - I will have to install mail client/check if this is installed? Once this is done I can remove that line from the script and check my mail for the error log from the crontab file is this automatic once i remove the code from the script and it will send a log file to my mail address once i figure all that out? thanks again for your help it is greatly appreciated.
Mentalist avatar
ve flag
I've clarified I have Mail installed, there were a number of error logs, if you have a scheduled task running every 2 minutes expect a number of emails haha, thanks for this i'll go through it from here and return! It seems to be unable to find python so going to try a few fixes thanks again folks!
user10489 avatar
in flag
Good luck and happy hunting!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.