【发布时间】:2020-06-03 03:18:17
【问题描述】:
所以我正在尝试使用谷歌云机器 24/7 运行我的 python 脚本,我想使用 tmux 和 cron,但我认为我做错了,它对我不起作用。当我在 SSH 窗口中运行我的脚本时,它运行良好,但是当我关闭它时。它不会发布到 twitter 或编辑我的 json 文件。
我尝试在谷歌中寻找答案,但没有运气。 我尝试过使用 tmux 并以无限循环和无锁运行我的脚本。 我尝试使用没有无限循环的 cron 并每 5 分钟运行一次我的脚本,但没有运气。 我曾尝试在 tmux 中使用 cron,但没有运气。
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 连接后,我写道: tmux,将目录更改为我的 python 文件和 json 文件所在的位置。我使用 python3 运行我的 python 脚本。我可以在控制台中看到我的新消息,也可以在 Twitter 上看到。我等了 5 分钟,每次在 reddit 上发布新内容时,我都可以在控制台中看到它并发布在 Twitter 上。我关闭了我的 SSH,并且在 twitter 或 json 文件中没有新帖子。我再次打开SSH,没有错误或任何东西。我也尝试了 cron:我使用了命令 '*/5 * * * * python 3 /python_programs/watch_for_new_TIL_facts.py' 当然没有''。仍然没有运气。 我在 tmux 的 cron 中尝试了这个命令,但没有它。我以普通用户和 root 的身份创建了一些东西。
【问题讨论】:
-
您能发布一下您是如何设置 cron 作业的吗?
-
'*/5 * * * * python 3 /python_programs/watch_for_new_TIL_facts.py'
-
但我也尝试过 // 在文件目录中
-
我建议你在几乎所有地方都实现一些日志记录和尝试语句来捕获错误。或者,先让应用在屏幕环境中运行,而不是放置 from 作业,以将任何错误保存在标准输出中
-
我已经在我自己的 PC 上运行了 12 小时,我有 0 个错误,我已经在谷歌云上运行了 X 小时,我没有问题,只有 tmux 和 cron 有问题。关闭 SSH 连接后,我的程序停止工作。
标签: python google-cloud-platform cron tmux