【问题标题】:elastic beanstalk cron seem dosen't working弹性豆茎 cron 似乎不起作用
【发布时间】:2020-11-16 10:36:20
【问题描述】:

我正在尝试使用 Elastic Beanstalk 部署网站。

每小时抓取一次并将其保存为 .txt 文件后,当用户访问它时,会加载 .txt 文件,但它似乎不起作用。

连接后,仅加载初始部署中的 .txt 文件。

当前的 .zip 配置是

-.ebextensions
     >cron-linx.config
-static
-templates
-application.py
-Enterprise.txt
-requirements.txt
-test.py

cron-linux.config

files:
    "/etc/cron.d/mycron":
        mode: "000644"
        owner: root
        group: root
        content: |
            */5 * * * * root /usr/local/bin/myscript.sh

    "/usr/local/bin/myscript.sh":
        mode: "000755"
        owner: root
        group: root
        content: |
            #!/bin/bash

            python3 /var/app/current/test.py

            exit 0

commands:
    remove_old_cron:
        command: "rm -f /etc/cron.d/mycron.bak"

test.py

import time
now = time.strftime('%H%M%S')
f=open('Enterprise.txt','w',encoding='UTF-8')
f.write(str(now))
f.close()

我该怎么办?对于Linux,如果您能详细解释一下,我将不胜感激,因为我只知道简单的命令

【问题讨论】:

    标签: python linux cron amazon-elastic-beanstalk


    【解决方案1】:

    您的cron 作业将在root 用户下运行,并将在/root 文件夹中创建Enterprise.txt。但是,您的应用程序在webapp 用户下运行并在/var/app/current 下运行。这就解释了为什么您找不到由cron 创建的Enterprise.txt(它将在/root 中)。

    要纠正该问题,您可以按如下方式更改您的 cron 脚本(我可以确认它适用于 Python 3.7 running on 64bit Amazon Linux 2/3.0.3):

    cron-linux.config

    files:
        "/etc/cron.d/mycron":
            mode: "000644"
            owner: root
            group: root
            content: |
                */5 * * * * root /usr/local/bin/myscript.sh
    
        "/usr/local/bin/myscript.sh":
            mode: "000755"
            owner: root
            group: root
            content: |
                #!/bin/bash
                
                exec &>> /tmp/cron_capture_log.txt
    
                /usr/sbin/runuser -l webapp -c 'cd /var/app/current; python3 ./test.py'
    
                exit 0
    
    commands:
        remove_old_cron:
            command: "rm -f /etc/cron.d/mycron.bak"
    

    【讨论】:

    • 你回来了,我的救星。复制并粘贴您的文件后,分发 5 分钟后,我查看了网页是否反映了新的 .txt,但没有任何反应:(
    • 您能登录实例并检查 /var/app/current 吗?
    • 我试过换行,但无济于事。让我们登录实例并转到 /var/app/current。
    • 我第一次知道这一点。我试试
    • Cron 测试目前是使用 Linux2 进行的。终于成功了!!非常感谢。现在,基于该代码,我必须每小时抓取一次以将结果保存在 .txt 文件中。祝你有美好的一天。
    猜你喜欢
    • 2020-06-15
    • 2023-04-07
    • 2018-02-04
    • 2015-03-20
    • 2016-01-04
    • 2019-01-02
    • 2018-02-25
    • 2019-11-06
    • 2018-03-23
    相关资源
    最近更新 更多