김준우
김준우

Reputation: 37

elastic beanstalk cron seem dosen't working

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

Upvotes: 1

Views: 142

Answers (1)

Marcin
Marcin

Reputation: 238051

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"

Upvotes: 1

Related Questions