Search code examples
cronamazon-elastic-beanstalk

Elastic Beanstalk cron import issue


I used Elastic Beanstalk to check that the code that saves the time as .txt every hour with the cron job works.

After that, I wrote the code that saves the time as saving the crawl result, but it did not work.

After a bit of debugging, it seems like an error that occurs because the module, such as from bs4 import BeautifulSoup, is not installed.

What should I do?

I am using the AL2 platform and below are test.py and cron-linux.config.

test.py

import time
from bs4 import BeautifulSoup

def function_test():
    now = time.strftime('%H%M%S')
    now_int = int(now) + 90000 # UTC TO KST 시차가 +9시간 발생
    now_kst = now_int % 240000 # 시간은 24시 넘어가면 안되므로 24시간으로 나누어 줌
    now_str = str(now_kst).zfill(6)    
        
    if 90000 < now_kst and now_kst < 160000:
        print("intime")
    else:
        print("outtime")


    f=open('Enterprise.txt','w',encoding='UTF-8')
    f.write(now_str)
    f.close()

function_test()

cron-linux.config

files:
    "/etc/cron.d/mycron":
        mode: "000644"
        owner: root
        group: root
        content: |
            */1 * * * * 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"

The current .zip configuration is

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

Solution

  • You can install bs4 using container commands:

    You can use the container_commands key to execute commands that affect your application source code. Container commands run after the application and web server have been set up and the application version archive has been extracted, but before the application version is deployed.

    Thus you can create new config in your .ebextensions.

    For example:

    .ebextensions/10_commands.config

    container_commands:
      10_install_bs4:      
        command: pip install bs4
      20_install_something_else:      
        command: pip install <other package>
    

    requirements.txt - easier alternative

    The easiest way would be to simply put all your pip requirements in the root folder of your app in requirements.txt file: