Crontab visible in logs but still doesn't seem to run? - amazon-web-services

I'm on an AWS server. I wrote a crontab and placed it on the server under /etc/cron.d. The contents of the crontab are the following:
SHELL=/bin/bash
PATH=/sbin:/bin:usr/sbin:/usr/bin
MAILTO=root
HOME=/
*/5 * * * * root <full-path-to-write-command> >> <full-path-to-txt-output-file>
After running sudo service crond restart, I check the logs by doing sudo tail -f /var/log/cron.
I can observe the cronjob in the logs:
<date-time-stamp> ip-<ip-address> CROND[12930]: (root) CMD (<full-path-to-write-command> >> <full-path-to-txt-output-file>)
However, when I check the <full-path-to-txt-output-file>, I don't see file being written to.
What could be the problem, if I see that the cronjob is executing? Thanks

Related

Cron Job is not running on EC2 Instance

I have searched the community questions before posting this and tried the suggestions to no avail.
I am confused as to why my cron jobs in my ec2 are not getting triggered.
I ran the following in my ec2:
sudo service crond start
sudo crontab -e
Here I added the following:
30 7 * * * root /usr/bin/python3 /home/ec2-user/hello-world.py
The python script creates a file on running. The job is not running as scheduled.
Please suggest me what I am missing here.

AWS CloudWatch log stream is empty while Docker container logs are full

I'm creating cron docker image, based on ubuntu image. I would use this tutorial,
but cron won't have PID 1, not 2 and not even 10. I found a solution to
run cron in foreground, check it using healthcheck and view logs of command using tail -f
Locally all is perfect, but when I deploy it to ECS Fargate logs are empty
Main issue
docker logs -f cron displays output, but
logs in cloudwatch are empty
Additional information
I created Dockerfile with following ENTRYPOINT and CMD
COPY crontab /etc/crontab
COPY script.sh /root/script.sh
ENTRYPOINT ["tini", "--", "/usr/bin/docker-entrypoint.sh"]
CMD prepare.sh tail -f /var/log/script.log
prepare.sh content:
# some preparation commands...
cron -L 15
exec "$#"
docker-entrypoint.sh also has at the end exec "$#".
crontab content:
*/3 * * * * root /root/script.sh >> /var/log/script.log
So, the last command is tail -f /var/log/script.log
In order for your container's logs to appear in CloudWatch logs, you need to setup your Task Definition to use the awslogs log driver.
Documentation on how to do this can be found here.

AWS crontab not working on Amazon Linux 2/3.0.1

I have a laravel project setup on AWS elastic beanstalk(Amazon Linux 2/3.0.1) and I'm trying to setup a cronjob to run a command.
I ran nano crontab -e and wrote
* * * * * cd /var/app/current && php artisan command:initcrawl to run for every minute as a test, but it doesn't work.
When i cd in /var/app/current and then run the command manually php artisan command:initcrawl it works without any problem.
My problem was the fact that I wrote 'nano' in front of the crontab command.
Instead of writing 'nano' in front of the command to use the nano editor i should have done this export EDITOR='/usr/bin/nano' then when i run crontab -e everything would work fine now.

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.

Amazon Web Services - cronjob not running every minute

I have a cronjob defined on an AWS ec2 (so it uses crontab). I did crontab -e and added this line into it:
*/1 * * * * /usr/bin/php /path/to/file/file.php
I saved it with vim and it says crontab: installing new crontab. I also made the permissions on the file 755, using chmod, and I put the proper shebang (#!/usr/bin/php -q) at the top of file.php. The script should update a database every minute, but I am not seeing the database get updated at all. What did I do wrong?
logging example:
*/1 * * * * >> /var/log//your_cron.log 2>&1