【问题标题】:How to add cronjob/scheduler for Python scripts on EC2 AWS?如何在 EC2 AWS 上为 Python 脚本添加 cronjob/调度程序?
【发布时间】:2021-06-14 00:16:05
【问题描述】:

我有一个关于我最近开发的 React 应用程序的问题。它基本上是一个登陆页面,它使用 React 前端和 Node+Express 后端,并从各个页面抓取数据(抓取器是用 Python 开发的)。

目前,React 应用程序本身托管在 Heroku 中,并且爬虫的执行正在运行,但它不是自动安排的。 当前设置如下:

  • 用于 Python 爬虫的 EC2
  • AWS RDS MYSQL 用于数据库,我将数据从 EC2 抓取工具写入其中

我创建了一个单独的文件来执行所有其他爬虫。

ma​​in.py

import time
import schedule
import os
from pathlib import Path
print('python script executed')


# make sure, what is the current working directory to add the right paths to scrapers
path = os.getcwd()
print(path)
#exec(open("/home/ec2-user/testing_python/lhvscraper.py").read())


filenames = [
    #output table: fundsdata
    Path("/home/ec2-user/testing_python/lhvscraper.py"),
    Path("/home/ec2-user/testing_python/luminorscrapertest.py"),
    Path("/home/ec2-user/testing_python/sebscraper.py"),
    Path("/home/ec2-user/testing_python/swedscraper.py"),
    Path("/home/ec2-user/testing_python/tulevascraper.py"),

    #output table: feesdata
    Path("/home/ec2-user/testing_python/feesscraper.py"),

    #output table: yield_y1_data
    Path("/home/ec2-user/testing_python/yield_1y_scraper.py"),

    #output table: navdata
    #Path("/home/ec2-user/testing_python/navscraper.py"),
]

def main_scraper_scheduler(): 
    print("scheduler is working")

    for filename in filenames:
        print(filename)
        with open(filename) as infile:
            exec(infile.read())

    time.sleep(11)

schedule.every(10).seconds.do(main_scraper_scheduler)

while True:
    schedule.run_pending()
    time.sleep(1)

我已经成功建立了MYSQL和EC2之间的连接,并在Putty上进行了测试-
这意味着,如果我执行我的 ma​​in.py,所有的抓取工具都在工作,将新数据插入到 MYSQL 数据库表中,然后再次重复(参见上面的代码)。唯一的问题是,当我关闭 Putty(终止连接)时,main.py 函数将停止运行。

所以我的问题是:如何设置它,以便 main.py 文件始终继续运行(比如说,每天中午 12 点运行一次) ) 没有我执行?

我知道这是关于设置 cron 作业或调度程序(或类似的东西),但我现在没有设法设置它,所以非常需要你的帮助。

提前致谢!

【问题讨论】:

  • 多久一次?简单的方法是将您的脚本添加到/etc/cron.hourly/etc/cron.daily。如果它需要不同的时间表,那么它只是使用crontab -e 添加一行,但您需要知道该行的格式。 man crontab 可以帮忙。
  • @TimRoberts 编辑了这个问题。例如,假设每天中午 12 点进行一次。
  • 您能否再解释一下:例如,如何设置这个/etc/cron.daily?实际上这是我的主要问题,如何设置 crontab ...
  • 我会添加一个答案来解释。

标签: python mysql amazon-web-services cron scheduled-tasks


【解决方案1】:

为避免使crontab 文件过长,Linux 提供了可以每小时、每天、每周或每月运行的预设条目。您无需修改​​任何 crontab 即可使用它。位于/etc/cron.hourly 中的任何可执行脚本将每小时自动运行一次。位于/etc/cron.daily 中的任何可执行脚本将每天自动运行一次(通常在上午 6:30),依此类推。只需确保包含 #! Python 行,chmod +x 使其可执行。请记住,它将以root 运行,您不一定能预测它将从哪个目录开始。不要做任何假设。

另一种方法是在您自己的个人 crontab 中添加一行。您可以使用crontab -l 列出您的crontab,也可以使用crontab -e 对其进行编辑。要每天中午运行一次,您可以添加:

0 12 * * *  /home/user/src/my_daily_script.py

【讨论】:

  • 好吧好吧。为了确保我理解它,我将再次解释您的解决方案。 1.创建一个目录/etc/cron.daily(现在一切都在/home/ec2-user/testing_python/ 中)并将脚本(在我的例子中,它是main.py)移动到这个目录。而已。如果我希望它每天运行。 2. 创建此命令以指定准确时间(crontab.guru 用于时间格式)。关于这个问题还有一个问题 - 如何保存和退出 crontab 编辑器? :) 无论如何,非常感谢@Tim!我试试看。
  • 您不必创建/etc/cron.daily,这些都已经存在。去看看。您需要sudo 才能复制到那里。 crontab 命令使用在EDITOR 环境变量中配置的任何编辑器。通常是vi。如果您不是vi 的人,请使用ZZ 保存并退出。
  • 好的,我找到了这些目录,例如/etc/cron.daily 和所有其他目录,那里有很多东西:)。还要感谢有关 Vim 的帮助。非常感谢,真的很感激!我会尝试两种方式。
  • 嗨@Tim。我想澄清这一点:我必须把这个make sure to include a #! line for Python 放在哪里?它是否必须在我正在执行的 Python 文件中(基于您的以下示例,home/user/src/my_daily_script.py
  • 喜欢:#! line for Python 必须包含什么内容?
猜你喜欢
  • 2019-03-23
  • 1970-01-01
  • 2018-08-30
  • 2023-04-06
  • 2021-10-13
  • 2018-09-10
  • 1970-01-01
  • 2021-06-24
相关资源
最近更新 更多