Search code examples
laravel-5amazon-ec2cronqueue

Laravel 5.4 Cron Job for Mail Queue not working on AWS EC2 instance


I have created a cron job on my EC2 instance for sending email from queue. It shows running the corn command every minute but I see tasks remain in my jobs table. I've already searched and tried existing solutions but failed to figure out the problem. Although queue working on my local server when I run php artisan schedule:run

My code in kernel:

$schedule->command('queue:work')
            ->everyMinute()
            ->withoutOverlapping();

Command I added in the file using crontab -e

* * * * * /usr/bin/php /var/www/html/prod_back/artisan schedule:run >> /dev/null 2>&1

This is the output I see in terminal when I run sudo service cron status

May 07 13:50:01 ip-172-31-39-27 CRON[20260]: pam_unix(cron:session): session opened for user ubuntu by (uid=0)
May 07 13:50:01 ip-172-31-39-27 CRON[20261]: (ubuntu) CMD (/usr/bin/php /var/www/html/prod_back/artisan schedule:run >> /dev/null 2>&1)
May 07 13:50:01 ip-172-31-39-27 CRON[20260]: pam_unix(cron:session): session closed for user ubuntu


Solution

  • Finally, I solved the problem. I got a community help that pointed out my cron command is wrong. I was running schedule command instead of queue command. So I changed it from

    * * * * * /usr/bin/php /var/www/html/prod_back/artisan schedule:run >> /dev/null 2>&1
    

    to

    * * * * * /usr/bin/php /var/www/html/prod_back/artisan queue:work >> /dev/null 2>&1
    

    One more thing, I also had to run below command to reload my env changes from sync to database for queue driver.

    /usr/bin/php /var/www/html/prod_back/artisan config:cache
    

    Please go to this link below and follow the checklist for diagnosing your specific problem in this regard.

    https://stackoverflow.com/questions/58738157/laravel-queue-with-aws-elastic-beanstalk