Search code examples
ruby-on-railsrubyamazon-web-servicescroncron-task

How do I get cron jobs running in container_commands on aws


I have a rails 3.2 app that uses ruby 1.9.3

We need to have a cron job run through container_commands.

Currently I have a common.config file in the .ebextensions directory with the following in it

container_commands:
  04_clear_old_epubs:
    command: "cat .ebextensions/clear_old_epubs_cron.txt > /etc/cron.d/clear_old_epubs_cron && chmod 644 /etc/cron.d/clear_old_epubs_cron"
    leader_only: true

Then in the clear_old_epubs_cron.txt file I have

*/5 * * * * root /usr/bin/php rm -rf /var/app/current/public/epub-download-1/* > /dev/null

#

I got this from the accepted answer in AWS Elastic Beanstalk, running a cronjob

The cron doesn't clear the folder like it's meant to. Not sure what is stopping the cron from running or if it is running but something else is wrong.

Can anyone see something that wrong or missing?


Solution

  • As you pointed out, it might be that the cron job isn't running at all, or it's failing due to some kind of error.

    One thing that will help will be to temporarily set [email protected] in the crontab. That way, cron will email you any output.

    Hopefully, that will produce some error message from your cronjob that will help you diagnose the problem.

    If you don't get any email, then the cron job isn't running at all (or your system can't send email...) At that point, you'll need to read up on your operating system's cron system and see if there's a step you missed.

    Another useful way to debug cron jobs can be to run them interactively via sudo, which should give you the same sort of environment as cron will use when it run the jobs in terms of paths, variables, etc.