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?

Ben Sinclair
  • 3,896
  • 7
  • 54
  • 94
  • Where is your database running ? RDS? – Rico Apr 13 '14 at 14:38
  • @Rico RDS. Is it some sort of permissions issue? – Ben Sinclair Apr 13 '14 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 Apr 13 '14 at 16:16
  • I just changed my code to do a simple file write and log each time the cron runs in a text file. And it works! So that means there is an issue with my original code when run by a code. Would the cron jobs have a different security group to the ec2 instance that would block the DB connection? – Ben Sinclair Apr 14 '14 at 01:15
  • Not really, the cronjob should just run with the same permissions as if you ran it from the command line as the same user specified in the cron job. Assuming you are using the same instance to run from the command line and as a cron job. – Rico Apr 14 '14 at 01:33
  • Thanks for your replies. Really do appreciate it :) Any recommendations on debugging this? I've turned on error reporting in PHP and on my EB application and I'm getting nothing in the snapshot logs error wise. The file runs but breaks as soon as it includes a settings file that includes other files and connects to RDS. I know this because I added the cod that writes to the text file before the include and after and only the first part gets written. It would be so handy to find our exactly what's breaking... – Ben Sinclair Apr 14 '14 at 01:36
  • Np :-) See if you can run your cron job successfully from another instance that has the same credentials and in the same security groups as the one running in your Elastic Beanstalk deployment. – Rico Apr 14 '14 at 01:52
  • OK. So just to confirm, am I using an instance in another application or just creating another environment within my application? At the moment I only have a single instance EB as I am in development mode so I'm not sure if you're referring to powering up another instance in my current environment – Ben Sinclair Apr 14 '14 at 02:06
  • Not an Elastic Beanstalk I mean a single EC2 instance where you can run your PHP script – Rico Apr 14 '14 at 02:11
  • 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? – Ben Sinclair Apr 14 '14 at 05: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! – Ben Sinclair Apr 14 '14 at 08:29

3 Answers3

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
lari
  • 752
  • 4
  • 19
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.

Community
  • 1
  • 1
Ankit Sharma
  • 5,191
  • 2
  • 23
  • 29
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']

Lucho
  • 1,024
  • 1
  • 15
  • 24