0
votes

i am trying to set up a cron job on one of my EC2 instances on AWS via crontab. Doing following steps:

  • Log into my server via ssh (with user ec2-user) and running crontab -l to check if any crontabs are running for existing users. There isn't any.
  • Run nano crontab -e to create a new crontab file for ec2-user. File created and saved. When running crontab -l getting output no crontab file for ec2-user and obviously cron is not running.

When running ps -ef | grep cron | grep -v grep cron seem to be running ok. Output is:

root 2460 1 0 13:12 ? 00:00:00 crond

I have had a look into a few resource on the web:

But still not working. Seems like something is wrong when creating a crontab file for ec2-user but i cannot seem to figure out what is wrong.

1
You need to run crontab -e....then you need to write your cron instructions, then press Escape then : and write wq(for write and quit)... - Hackerman
Thanks for the tip, @Hackerman. Just done that and all seem to worked fine - got the message "crontab installed". But the job still does not run for some reason. And when i do crontab -l i'm not getting anything back at the moment. Not quite sure what to do now. Thanks. - Sky21.86
For some reason when i add my cron job /5 * * * * php /var/www/html/scheduled.php > /dev/null 2>&1 - the first "" is not getting populated. Might this be the reason? - Sky21.86
So the job is not running? Try to redirect stdout and stderr to a file, like: /5 * * * * php /var/www/html/scheduled.php >> ~/tmp.log 2>&1 and check if there is some kind of error. - Danilo
Hello Danilo, thanks for your help. So i should just update my crop job to look like this: */5 * * * * php /var/www/html/scheduled.php >> ~/tmp.log 2>&1 and it will create a log with any errors. I am wondering if there is any other way for me to check if my cron is set up correctly - Sky21.86

1 Answers

0
votes

It is best to add cron file (any name without dot, e.g. my-cron-job) the content is just the cron command make sure there is \r in the end of the command add this file under /etc/cron.d permissions 644

This approach is used in all automations in Ansible / Puppet