Laravel Scheduling Task on AWS is not working
Asked Answered
G

1

5

I am deploying my Laravel application to ElasticBeanstalk environment. I am trying to run a command in the Scheduling Task on the server. But it is not working. This is what I have done.

I schedule my command in the Kernel.php file as follow.

$schedule->command('counter:update')->everyMinute()->onOneServer();

I am using Redis as my cache driver and it is working.

Then I tried two different approaches.

First Approach: Using Laravel AWS Worker package, https://packagist.org/packages/dusterio/laravel-aws-worker

I installed the package following the instructions mentioned in the doc.

Then I whitelist the worker routes mentioned in the doc in the CSRF middleware.

Then I created a cron.yml file right inside the project's root folder with the following content.

version: 1
cron:
  - name: "schedule"
    url: "/worker/schedule"
    schedule: "* * * * *"

Then I deployed my application. But the Scheduling task is not working. So I tried the second approach as follow.

Second Approach: Using Crontab

I created a .ebextensions/cronjob.config file with the following content.

files:
    "/etc/cron.d/schedule_run":
        mode: "000644"
        owner: root
        group: root
        content: |
            * * * * * root . /opt/elasticbeanstalk/support/envvars && /usr/bin/php /var/www/html/artisan schedule:run 1>> /dev/null 2>&1

commands:
    remove_old_cron:
        command: "rm -f /etc/cron.d/*.bak"

Then I deployed my application. The second approach is not working either. What is missing in my configuration and how can I get it working?

This is my another cronjob.config file.

files:
    "/etc/cron.d/mycron":
        mode: "000644"
        owner: root
        group: root
        content: |
            * * * * * root /usr/local/bin/myscript.sh

    "/usr/local/bin/myscript.sh":
        mode: "000755"
        owner: root
        group: root
        content: |
            #!/bin/bash

            date > /tmp/date
            * * * * * root . /opt/elasticbeanstalk/support/envvars && /usr/bin/php /var/www/html/artisan schedule:run 1>> /dev/null 2>&1

            exit 0

commands:
    remove_old_cron:
        command: "rm -f /etc/cron.d/mycron.bak"
Giesecke answered 14/7, 2020 at 0:29 Comment(0)
M
11

The cron.yml you tried is only for worker environments.

The /opt/elasticbeanstalk/support/envvars is only for Amazon Linux 1. Since you are using 64bit Amazon Linux 2 v3.0.3 running PHP 7.3 (as I recall), the cron will error out.

Thus, you should focus on the second approach, which is in line with AWS blog on how to use cron on AL2:

Therefore, I would recommend execute cron job through a script (e.g., /usr/local/bin/myscript.sh) as shown in the blog.

Modified content for /usr/local/bin/myscript.sh.

You can try first only with date > /tmp/date just to check if the cron job works. For this you have to deploy your application, and check if /tmp/date contains the time generated by this script.

    "/usr/local/bin/myscript.sh":
        mode: "000755"
        owner: root
        group: root
        content: |
            #!/bin/bash

            date > /tmp/date

            # the line below is commented out.
            #/usr/bin/php /var/www/html/artisan schedule:run 1>> /dev/null 2>&1

            exit 0

Access environment variables in Amazon Linux 2

The env variables are stored in /opt/elasticbeanstalk/deployment/env

Thus to load them in your script you can use the following:

env $(cat /opt/elasticbeanstalk/deployment/env | xargs) sh -c '/usr/bin/php /var/www/html/artisan schedule:run 1>> /dev/null 2>&1'

or in two lines

export $(cat /opt/elasticbeanstalk/deployment/env | xargs) 
/usr/bin/php /var/www/html/artisan schedule:run 1>> /dev/null 2>&1
Mcripley answered 14/7, 2020 at 0:55 Comment(5)
Comments are not for extended discussion; this conversation has been moved to chat.Immoderate
@SamuelLiew what are comments for if not for extended discussion?Caines
Spent hours trying to figure out why my scheduler wasn't working anymore after a plateform update. Thanks for pointing to the right env var path!Devereux
@FabriceLefloch No problem. Glad it worked out:-)Mcripley
I have been chasing this forever. This is the only solution that works. I would vote this up 100 times if I could.Whistler

© 2022 - 2024 — McMap. All rights reserved.