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.
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
After several efforts I found a alternative way to run cron job easily
Create a route
routes/web.php
create a function in the HomeController
Run the URL every minute using https://cron-job.org/en/
Try to reload cron service after new cron file is created.
service cron reload
or something like that for OS your EB is using.
This is how I did it on EBS:
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:
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.