4

This is my very first time running a cron job on Elastic Beanstalk (EB). After deploying my code, it seems the cron job is created and running but the PHP script is not executing correctly. Here's my set-up.

In my .ebextensions folder I have a file called 01run.config.

container_commands:
  01_remove_old_cron_jobs:
    command: "crontab -r || exit 0"
  02_cronjobs:
    command: "cat .ebextensions/cron_jobs.txt > /etc/cron.d/cron_job && chmod 644 /etc/cron.d/cron_job"
    leader_only: true

In my .ebextensions folder I also have a cron_jobs.txt file. Please note that I have an line break at the end of this file as instructed by another stackoverflow post. In my example below I am running the command as ec2-user but I also tried root.

* * * * * ec2-user /usr/bin/php -q /var/app/current/tests/cron.php

After deploying my code, I can see that the file /etc/cron.d/cron_job has been created. I can also see the cron job running every minute when I run sudo tail /var/log/cron.

[ec2-user@ip-xxx-xxx-xxx-xxx ~]$ sudo tail /var/log/cron
Apr 13 12:54:53 ip-xxx-xxx-xxx-xxx crontab[26093]: (root) DELETE (root)
Apr 13 12:55:01 ip-xxx-xxx-xxx-xxx crond[1230]: (*system*) RELOAD (/etc/cron.d/cron_job)
Apr 13 12:55:01 ip-xxx-xxx-xxx-xxx CROND[26128]: (ec2-user) CMD (/usr/bin/php -q /var/app/current/tests/cron.php)
Apr 13 12:56:01 ip-xxx-xxx-xxx-xxx CROND[26139]: (ec2-user) CMD (/usr/bin/php -q /var/app/current/tests/cron.php)

Within /var/app/current/tests/cron.php I have some code that adds a row to a MySQL database (hosted on RDS). But nothing is being added to the database.

I then tried running the cron command directly through my terminal window:

$ /usr/bin/php -q /var/app/current/tests/cron.php

And it runs without error and adds the record to the database. I am logged in as ec2-user in terminal.

Have I missed something? Or is my cron job code set-up incorrectly?

11
  • Where is your database running ? RDS?
    – Rico
    Commented Apr 13, 2014 at 14:38
  • @Rico RDS. Is it some sort of permissions issue? Commented Apr 13, 2014 at 15:38
  • Could be. Looks pretty bizarre, do you mind posting the code for cron.php ? Also, is cron.php spitting out any debug info ?
    – Rico
    Commented Apr 13, 2014 at 16:16
  • 1
    Ah ha! I found the issue. My system relies on the Environment variable PARAM1 to determine database information. When the cron job runs, these variables aren't passed. Would you know if this is possible at all? Or am I going to have to come up with another solution? Commented Apr 14, 2014 at 5:49
  • 1
    I've ended up using wget to run the cron which seems to add the variables in. So I think I'm good to go :) Thanks for your help! Commented Apr 14, 2014 at 8:29

3 Answers 3

4

I had a similar problem with a php script that was trying to access an AWS RDS database. Is your php script getting the database details with $_SERVER['RDS_xxxx']? If so, those RDS_xxxx variables don't exist in the environment when the php script is run by cron.

In order to fix this, I added the variables to the beginning of the cron file:

RDS_HOSTNAME=<my_database_hostname>
RDS_PORT=<my_database_port>
RDS_USERNAME=<my_database_username>
RDS_PASSWORD=<my_database_password>
RDS_DB_NAME=<my_database_name>

* * * * * php /path/to/my/script.php
2

Login via SSH and check if generated cron job file/etc/cron.d/cron_job have unix line ending i.e. ASCII text not win i.e. ASCII text, with CRLF line terminators.

To check the line ending refer the answer here.

Note: If you have windows line ending then you will have to convert the line ending of file .ebextensions/cron_jobs.txt, for that you can use dos2unix or similar program.

1

I had a similar problem with my RDS_ variables on AWS, I followed this discussion and it works. This was my cronjob before:

RDS_HOSTNAME=<my_database_hostname> 
RDS_PORT=<my_database_port>
RDS_USERNAME=<my_database_username>
RDS_PASSWORD=<my_database_password> 
RDS_DB_NAME=<my_database_name>
* * * * * cd /var/app/current && bin/cake notifications send_push >> /var/tmp/notifications.log 2>&1

And changed to this:

* * * * * . /opt/elasticbeanstalk/support/envvars cd /var/app/current && bin/cake notifications send_push >> /var/tmp/notifications.log 2>&1

And now I can access them like: $_SERVER['RDS_HOSTNAME']

Not the answer you're looking for? Browse other questions tagged or ask your own question.