Tmux,cron不能与我的python脚本在google clouds上工作。

问题描述 投票:0回答:1

所以我试图运行我的python脚本247使用谷歌云机,我想使用tmux和cron,但我认为我正在做一些错误的事情,它不为我工作。当我在SSH窗口中运行我的脚本时,它工作得很完美,但当我关闭它时。它不能发布到twitter或编辑我的json文件。

我试着在google中寻找答案,但没有找到。我试过使用tmux,并以无限循环和无锁的方式运行我的脚本,我试过使用无无限循环的cron,并每5分钟运行我的脚本,但没有运气。

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

我想让它每5分钟运行一次,但没有运气,在我关闭SSH后,它不会将新的东西发布到twitter上。打开我的SSH连接到google cloud后,我写道:tmux,改变目录到我的python文件和json文件所在的地方,我使用python3运行我的python脚本。我可以在控制台看到我的新新闻,我可以在twitter上看到它。我等了5分钟,每次有新的东西发布在reddit上,我都能在我的控制台看到它,并发布在twitter上。我关闭SSH,twitter上和json文件里都没有新的帖子。我再次打开SSH,没有任何错误或任何东西。我也试过cron:我用命令'*5 * * * * python 3 python_programswatch_for_new_TIL_facts.py'当然没有''。我已经创建的东西作为普通用户和根。

python google-cloud-platform cron tmux
1个回答
0
投票

在你的crontab中,你有一个空格之间的 Python3. 消除它。

© www.soinside.com 2019 - 2024. All rights reserved.