有弹性的豆茎cron似乎不起作用 [英] elastic beanstalk cron seem dosen't working

查看:63
本文介绍了有弹性的豆茎cron似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to deploy a website using Elastic Beanstalk.

每小时进行一次爬网并将其保存为.txt文件后,当用户访问该文件时,该.txt文件会加载,但似乎无法正常工作.

After crawling every hour and saving it as a .txt file, when the user accesses it, the .txt file loads but it doesn't seem to work.

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

When connected, only the .txt file from the initial deployment is loaded.

当前.zip配置为

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

cron-linux.config

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,如果您能详细解释它,我将不胜感激,因为我只知道简单的命令

What should I do? For Linux, I would appreciate it if you could explain it in detail, as I only know simple commands

推荐答案

您的 cron 作业将在 root 用户下运行,并创建 Enterprise.txt /root 文件夹中的code>.但是,您的应用程序在 webapp 用户下运行,并在/var/app/current 下运行.这说明了为什么找不到由 cron 创建的 Enterprise.txt (它将位于/root 中)的原因.

Your cron job will run under root user and will create Enterprise.txt in the /root folder. However, you application runs under webapp user and operates in /var/app/current. This explains why you can't find Enterprise.txt created by the cron (it will be in /root).

要解决此问题,您可以按如下所示更改cron脚本(我可以确认,该脚本可在运行于64位Amazon Linux 2/3.0.3的Python 3.7上运行):

To rectify the issue, you could change your cron script as follows (I can confirm it works on 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"

这篇关于有弹性的豆茎cron似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆