0

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

app\Console\Kernel.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"
Harshith J Poojary
  • 316
  • 10
  • 26
  • Is `/laralog.log` the correct path? Surely you don't intend to write the log to the filesystem's root? Consider using `eb ssh` (https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb3-ssh.html) to log into the server and try executing the command *there*. – ceejayoz Jan 19 '22 at 17:56
  • After root, you have a dot(`.`) and a space before `/opt/..`. Is it a mistake in copying? – Riz Jan 19 '22 at 23:38
  • @ceejayoz I found cron-setup.config code from this blog https://medium.com/qosoor/the-ultimate-guide-to-setup-cron-jobs-with-laravel-elastic-beanstalk-d497daaca1b0 – Harshith J Poojary Jan 20 '22 at 03:28
  • @Riz I didn't make any changes copied code from blog – Harshith J Poojary Jan 20 '22 at 03:28
  • @HarshithVA, remove the `space` between `.` and `/opt/..` or use `source /opt/elasticbeanstalk/support/envvars`. – Riz Jan 20 '22 at 08:07
  • @Riz Not working – Harshith J Poojary Jan 21 '22 at 14:00
  • @HarshithVA, can you access thr `/laralog.log` logs? what do you see there? maybe change that to `/var/laralog.log` as well. – Riz Jan 21 '22 at 23:35
  • There is no file called laralog.log https://i.ibb.co/9q6k0VF/Screenshot-2022-01-22-085628.png – Harshith J Poojary Jan 22 '22 at 03:26
  • I am using cloudwatch for logging – Harshith J Poojary Jan 22 '22 at 03:30
  • @HarshithVA, but your `artisan` command fails. In your cloudwatch logs, you can see `artisan is not defined.` If `artisan` is not defined and doesn't work, how it's gonna 'run' `schedule:run`? In other words, you have the schedule but it's not running. I am not familair with Laravel, I might be wrong.. – Riz Jan 22 '22 at 23:43
  • the @Riz is right, the artisan command is not found in this path. Is a option navigate to the folder of the project: "cd /var/app/current/&& sudo php artisan schedule:run >> /dev/null 2>&1" . Obviously "sudo" is not ideal but you can try for test. If necessary, change php for /usr/bin/php – Lucas Pace Jan 23 '22 at 20:19

3 Answers3

0

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

service cron reload

or something like that for OS your EB is using.

Tomasz Breś
  • 481
  • 2
  • 5
0

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.

Mtxz
  • 3,749
  • 15
  • 29
  • What changes I need to do in order to implement it in my elasticbeanstalk – Harshith J Poojary Jan 26 '22 at 04:49
  • Just remove the log part if it does not suit you. What you need to test is my first code part. Adapt paths and users to your setup. In the end, it'll create an SH script running the artisan command, and the CRON will run the script. – Mtxz Jan 26 '22 at 12:19
0

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/

Harshith J Poojary
  • 316
  • 10
  • 26