Elastic Beanstalk Linux 2 Cron Job running but not executing - django

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.

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! :)

Related

Cron job Django Elastic Beanstalk

I am trying to set a cron job on my project to send a email after 15 minutes.
This is in django.config
04_cronjb:
command: "cat .ebextensions/cron-linux.config > /etc/cron.d/crontab && chmod 644 /etc/cron.d/crontab"
leader_only: true
This is my cron-linux.config file
files:
"/etc/cron.d/mycron":
mode: "000644"
owner: root
group: root
content: |
* * * * * source /opt/python/current/env && python /opt/python/current/app/manage.py cronjb
It all deploys successfully but i am not receiving any email.
Cronjb script is working i have tested it. So the error is one these two files.
Is there some mistake in it?
Your 04_cronjb copies entire content of cron-linux.config into crontab. This is sadly incorrect.
Instead you should do as shown here. This includes putting all the bash commands you want to execute in a custom script called, e.g., myscript.sh and then adding myscript.sh to cron only.

How to run wget cron command on elastic beanstalk config

I have one instance on elastic Beanstalk.
According to the documentation for running the cron on beanstalk environment I would create a file named something like "mycron.config" and place it in the .ebextensions folder.
I've done that.
The "mycron.config" file read like this:
commands:
command: "*/5 * * * * /usr/bin/wget -O /dev/null https://example.com/pull-shopify-orders"
I'm attempting to hit this url every 5 minutes.
The cron isn't running. What am I doing wrong?
Not sure what documentation you are referring to (no link), but I guess you write about cron.yaml which is only for worker environments.
To setup cron on web environments, you have to create /etc/cron.d/mycron "manually" with proper content and permissions.
This procedure is explained in a very recent AWS blog post:
How do I create cron jobs on Amazon EC2 instances in Elastic Beanstalk environments?
The blog creates cron-linux.config with the exemplary content of (modified content for your use-case):
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
#date > /tmp/date
/usr/bin/wget -O /dev/null https://example.com/pull-shopify-orders
exit 0
commands:
remove_old_cron:
command: "rm -f /etc/cron.d/mycron.bak"

How to set environment variables in Amazon Elastic Beanstalk when running a cron job (Node.js)?

I have been working on configuring a cron job while deploying an environment on Elastic Beanstalk. The purpose of the job is to run every 30 minutes and execute a Node.js script that processes SMS schedules in our system; when a schedule is ready, an SMS message is sent via the Twilio api.
Unfortunately, Node.js environments don't have the file /opt/elasticbeanstalk/support/envvars containing the environment variables defined (Maybe I am missing something?).
To work around this issue, I am loading the environment variables from /opt/elasticbeanstalk/bin/get-config in Python and then executing my Node.js script.
Everything is working as expected so hopefully this can help someone in the same situation; however, I am wondering if there is a better way to accomplish this... Open to suggestions.
In my .ebextensions folder I have the config file for the cron:
files:
"/etc/cron.d/process-sms-schedules":
mode: "000644"
owner: root
group: root
content: |
*/30 * * * * root /usr/local/bin/process-sms-schedules.sh
"/usr/local/bin/process-sms-schedules.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
# Execute script to process schedules
python /var/app/current/process-sms-schedules.py > /var/log/process-sms-schedules.log
exit 0
commands:
remove_old_cron:
command: "rm -f /etc/cron.d/*.bak"
Here is the Python script that gets executed as part of the cron job:
#!/usr/bin/env python
import os
import subprocess
from subprocess import Popen, PIPE, call
import simplejson as json
envData = json.loads(Popen(['/opt/elasticbeanstalk/bin/get-config', 'environment'], stdout = PIPE).communicate()[0])
for k, v in envData.iteritems():
os.environ[k] = v
call(["babel-node", "/var/app/current/process-sms-schedules.js"])
Thanks in advance for any feedback.
References
Cron Job Elastic Beanstalk
How to set environment variable in Amazon Elastic Beanstalk (Python)
I had this issue while trying to execute a PHP file inside an Elastic Beanstalk environment. In particular I was trying to execute wp-cron.php file.
Basically you should write a cron job like this:
/etc/cron.d/wp-cronjob :
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/.local/bin:/home/ec2-user/bin
*/5 * * * * ec2-user . /opt/elasticbeanstalk/support/envvars; /usr/bin/php /var/www/html/wp-cron.php > /dev/null 2>&1
Explained:
Every 5 minutes executes commands as user "ec2-user": '*/5 * * * * ec2-user'
Loads elasticbeanstalk environment variables: '. /opt/elasticbeanstalk/support/envvars;'.
Do not print any output: '> /dev/null 2>&1'.
Also:
.ebextensions/wp-cronjob.txt
# Load paths
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/.local/bin:/home/ec2-user/bin
# Every 5 minutes executes commands as user "ec2-user": '*/5 * * * * ec2-user'.
# Loads elasticbeanstalk environment variables: '. /opt/elasticbeanstalk/support/envvars;'.
# Do not print any output: '> /dev/null 2>&1'.
*/5 * * * * ec2-user . /opt/elasticbeanstalk/support/envvars; /usr/bin/php /var/www/html/wp-cron.php > /dev/null 2>&1
.ebextensions/cronjob.config
container_commands:
add_wp_cronjob:
command: "cat .ebextensions/wp-cronjob.txt > /etc/cron.d/wp-cronjob && chmod 644 /etc/cron.d/wp-cronjob"
leader_only: true
commands:
remove_old_cron:
command: "rm -f /etc/cron.d/*.bak"
Maybe is not the same for a Node.js environment but I am pretty sure they are similar.

.config file is being ignored by elastic beanstalk AWS

I have a web app running on elastic beanstalk. For some reason I was able to install the composer files in order to run my laravel app. The problem is that no other config file works. I have put newrelic.config into the .ebextensions/ directory, but that file got ignored.
I recently tried to create a cron job using this, AWS Elastic Beanstalk, running a cronjob, but it is not working.
Example of a .config file:
container_commands:
01_some_cron_job:
command: "cat .ebextensions/some_cron_job.txt > /etc/cron.d/some_cron_job && chmod 644 /etc/cron.d/some_cron_job"
leader_only: true
When I ssh into the ec2 instance, there is no such directory as some_cron_job.
The source gets committed to beanstalk, but beanstalk is not running the commands.
How can I make beanstalk acknowledge the .config files. Fixing this cronjob will also fix installing new relic, because both configs are being ignored and I do not know why.
Try putting it in commands section. It is more of a server command than a container command.
commands:
01_some_cron_job:
command: "cat .ebextensions/some_cron_job.txt > /etc/cron.d/some_cron_job && chmod 644 /etc/cron.d/some_cron_job"
leader_only: true
I had similar issues as well using container_commands and files, however, I deferred to the files event and it worked like a charm. My specific setup is as below.
.ebextensions/cron.config
files:
"/etc/cron.d/mycronstuff":
mode: "000644"
owner: root
group: root
content: |
# Run daily job every 8 hours
0 */8 * * * root curl -i XXXXXXXXXX
# Run nightly job at 2AM (8AM UTC)
0 8 * * * root curl -i XXXXXXXXX
Update 2019:
You must use a cron.yaml file on your project directory.
inside the file you can mention:
version: 1
cron:
- name: "task1"
url: "/scheduled"
schedule: "* * * * *"

AWS Elastic Beanstalk: .ebextensions for a specific Environment

I have two Environments at AWS Elastic Beanstalk: Development and Production.
I would like that .ebextensions/app.config only run on Production environment.
Theres any way to do that?
app.config:
container_commands:
01-command:
command: "crontab .ebextensions/cronjob"
leader_only: true
According to TNICHOLS idea I found a solution:
Change the environment PARAM1 variable value to MyAppEnv-Production (or what you want).
app.config:
container_commands:
command-01:
command: "/bin/bash .ebextensions/crontab.sh"
leader_only: true
crontab.sh:
if [ "$PARAM1" == "MyAppEnv-Production" ]; then
crontab -l > /tmp/cronjob
#CRONJOB RULES
echo "00 00 * * * /usr/bin/wget http://localhost/cronexecute > /dev/null 2>&1" >> /tmp/cronjob
crontab /tmp/cronjob
rm /tmp/cronjob
echo 'Script successful executed, crontab updated.'
else
echo 'This script is only executed in the production environment.'
fi
I don't think there's a simple way to do it in the way you're thinking. You could have the config file run and execute a second script (perhaps cron.sh). Inside cron.sh you can check the environment name and then add the cronjobs accordingly. Haven't tested it, but I think that should work.