so i am trying to run my python script 24/7 using google cloud machine, I want to use tmux and cron but i think i am doing something wrong and it doesn't work for me. When i run my script in SSH window it works perfectly but when i close it. It doesn't post to twitter or edit my json file.
I tried looking for answer in google but no luck for me. I have tried using tmux and running my script with infinite loop and no lock. I have tried using cron without infinite loop and run my script every 5 minutes and no luck. I have tried using cron inside tmux and no luck.
import requests
import json
import time
import twitter
class TodayILearned():
def __init__(self):
self.link = 'https://www.reddit.com/r/todayilearned/new/.json'
def get_info(self):
try:
r = requests.get(self.link, headers = {'User-agent': 'your bot 0.1'})
r.raise_for_status()
except requests.exceptions.HTTPError as error:
print(f'There is problem:\n{error}')
return False
new_til = json.loads(r.content)
new_til = new_til["data"]["children"][0]['data']['title']
new_til = new_til.replace('TIL', '').replace('Til', '').strip()
for _ in range(len(new_til) - 1):
if new_til[0].isalpha() == False:
new_til = new_til.replace(new_til[0], '').strip()#.capitalize()
else:
break
new_til = new_til.split(' ', 1)
if new_til[0].lower() == 'this' or new_til[0].lower() == 'that' or new_til[0].lower() == 'about' or new_til[0].lower() == 'of':
new_til.pop(0)
new_til = ' '.join(new_til)
new_til = new_til[:1].upper() + new_til[1:]
if new_til[-1].isalnum() == True:
new_til += '.'
return new_til if len(new_til) < 280 else False #change for 140 when twitter working
def save_new_dict(self, new_dict):
with open('til_news_base.json', 'w') as json_file:
json.dump(new_dict, json_file, indent=2)
def read_json_file(self):
with open('til_news_base.json') as json_file:
data = json.load(json_file)
self.last_key = int(sorted(list(data.keys()))[-1])
return data
def post_on_twitter(self, new_post):
TOKEN = 'xxx'
TOKEN_KEY = 'xxx'
CON_SEC = 'xxx'
CON_SEC_KEY = 'xx'
my_auth = twitter.OAuth(TOKEN,TOKEN_KEY,CON_SEC,CON_SEC_KEY)
twit = twitter.Twitter(auth=my_auth)
twit.statuses.update(status=new_post)
def program(self):
wait_for_seconds = 30
while True:
#first load the base from file
dict_with_news = self.read_json_file()
#second get new posts from reddit
new_info = self.get_info()
#check if new post in base or if is it last post
if new_info != False:
if new_info != dict_with_news[str(self.last_key)]:
dict_with_news[str(self.last_key + 1)] = new_info
print(new_info)
#add to base if not
self.save_new_dict(dict_with_news)
#print new TIL on twitter
self.post_on_twitter(new_info)
#wait Xs
time.sleep(wait_for_seconds)
def program():
class_til = TodayILearned()
class_til.program()
if __name__ == "__main__":
program()
I want it to run every 5 minutes but no luck, it doesn't post new stuff to twitter after i close SSH. after opening my SSH connection to google cloud i write: tmux, change directory to place where my python file and json file is. i run my python script using python3. I can see in console my new news and i can see it on twitter. I am waiting for 5 minutes and every time new stuff is posted on reddit i can see it in my console and posted on twitter. I close my SSH and there are no new posts on twitter or in json file. I open SSH again and there are no errors or anything. I tried also cron: i used command '*/5 * * * * python 3 /python_programs/watch_for_new_TIL_facts.py' without '' of course. and still no luck. I tried this command in cron in tmux and without it. I have created stuff as normal user and as root.
In your crontab, you have a space between Python
and 3
. Eliminate it.