elastic beanstalk cron seem dosen't working

六月ゝ 毕业季﹏ 提交于 2021-01-28 12:16:02

问题


I am trying to deploy a website using Elastic Beanstalk.

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.

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

The current .zip configuration is

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

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


回答1:


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

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"


来源:https://stackoverflow.com/questions/63109524/elastic-beanstalk-cron-seem-dosent-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!