skip to Main Content

I have deployed a laravel application in Elastic Beanstalk with Load Balancing. I have to backup my database daily and store it in s3 bucket so I am using Laravel-backup-server package. And I have set up cronjob using Nginx. When I manually run php artisan schedule:run in my local machine it works fine but when I deploy to Aws it’s not running the cron job. My setup looks something like this

.ebextensions/cron-setup.config

"/etc/cron.d/cron_example":
    mode: "000644"
    owner: root
    group: root
    content: |
      * * * * * root . /opt/elasticbeanstalk/support/envvars && /usr/bin/php /var/www/html/artisan schedule:run 1>> /laralog.log 2>&1
commands:
  rm_old_cron:
    command: "rm -fr /etc/cron.d/cron_example.bak"
    ignoreErrors: true

appConsoleKernel.php

protected function schedule(Schedule $schedule)
{
    $schedule->command('backup:run --only-db')
    ->everyMinute();
    Log::info('running cron');
    
}

I am using cloudwatch for logging. And when I run php artisan schedule:run locally I am getting a log in cloud watch. But when I deploy it to Elastic beanstalk and set up cron in ngnix there is no log.

enter image description here

Also, I tried to use this config which I found in Github I didn’t make any changes but not working

    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"

3

Answers


  1. Chosen as BEST ANSWER

    After several efforts I found a alternative way to run cron job easily

    Create a route

    routes/web.php

    Route::get('/cron/run',[HomeController::class, 'cron'])->name('cron');
    

    create a function in the HomeController

    public function cron()
    {
        Artisan::call("schedule:run");
        return 'run cron successful';
    }
    

    Run the URL every minute using https://cron-job.org/en/


  2. Try to reload cron service after new cron file is created.

    service cron reload

    or something like that for OS your EB is using.

    Login or Signup to reply.
  3. This is how I did it on EBS:

    files:
    "/etc/cron.d/artisan-scheduler":
        mode: "000644"
        owner: root
        group: root
        content: |
            * * * * * webapp /usr/local/bin/artisan-scheduler-cron.sh
    
    "/var/log/laravel-schedule.log":
        mode: "000755"
        owner: webapp
        group: webapp
        content: |
            created
    
    "/usr/local/bin/artisan-scheduler-cron.sh":
        mode: "000755"
        owner: webapp
        group: webapp
        content: |
            #!/bin/bash
    
            /usr/bin/php /var/app/current/artisan schedule:run >> /var/log/laravel-schedule.log 2>&1
            exit 0
    
    commands:
    01_mkdir_webapp_dir:
        # use the test directive to create the directory
        # if the mkdir command fails the rest of this directive is ignored
        test: "mkdir /home/webapp"
        command: "ls -la /home/webapp"
    02_chown_webapp_dir:
        command: "chown webapp:webapp /home/webapp"
    03_chmod_webapp_dir:
        command: "chmod 700 /home/webapp"
    remove_old_cron:
        command: "rm -f /etc/cron.d/artisan-scheduler.bak"
    

    In the end, I make the artisan call from a script and run the script in the Cron job. Laravel schedules are declared as usual in the Kernel.php. Note that the "log" parts are not relevant to your issue.

    You could also try a simple scheduled task to check if the scheduler work:

    $schedule->call(function () {
            Log::info('[SCHEDULE] test schedule');
        })
            ->name('test-schedule')
            ->withoutOverlapping(720) //12h
            ->everyMinute()
            ->onOneServer();
    

    Also, SSH to your server and check other logs. There may be another error on the artisan command, but on the "shell" level, it does not reach PHP/Laravel, and so no logs in Cloudwatch about it.

    Sources: 1, 2 & 3.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search