insite
insite

Reputation: 93

Elastic Beanstalk Linux 2 Cron Job running but not executing

I have put my configuration files down to as simple of an example as I can think of, but I still can't get the cron to execute. This is a django project, and though it looks like the crons are trying to run, they are not actually executing.

.ebextensions/cron-log.config    
  "/etc/cron.d/test_cron":
    mode: "000644"
    owner: root
    group: root
    content: |
        */1 * * * * root . /opt/elasticbeanstalk/deployment/env && echo "TESTING" >> /var/log/test_log.log 2>&1

commands:
  rm_old_cron:
    command: "rm -fr /etc/cron.d/*.bak"
    ignoreErrors: true

when downloading the logs from aws, test_log.log does not exist

in the cron file returned in the logs, it shows:

Oct 29 09:03:01 ip-172-31-8-91 CROND[10212]: (root) CMD (. /opt/elasticbeanstalk/deployment/env && echo "TESTING" >> /var/log/test_log.log 2>&1)

I have tried many variations of this including having the command that is ran make changes in our database, but it never seems to actually execute.

Upvotes: 2

Views: 442

Answers (1)

DashRantic
DashRantic

Reputation: 1518

I ran into this same issue recently (PHP ElasticBeanstalk, migrating to AL2), and found this solution.

Basically since Amazon Linux 2 no longer has "/opt/elasticbeanstalk/support/envvars", and "/opt/elasticbeanstalk/deployment/env" requires sudo to access, you'll need to effectively create your own environment variables file for your cron jobs to reference. Create a new .ebextensions config script containing the following:

commands:
    setvars:
        command: /opt/elasticbeanstalk/bin/get-config environment | jq -r 'to_entries | .[] | "export \(.key)=\"\(.value)\""' > /etc/profile.d/sh.local

Then, reference the "/etc/profile.d/sh.local" file instead of "/opt/elasticbeanstalk/deployment/env". Your cron entry should now look like:

*/1 * * * * root . /etc/profile.d/sh.local && echo "TESTING" >> /var/log/test_log.log 2>&1

...and you should be good to go! :)

Upvotes: 2

Related Questions