Search code examples
laravelcronscheduled-taskscron-task

Laravel CRON is working only by command manually, and not trigger the command everyMinute


I already have laravel 5.4 app in AWS Ubuntu, in my project I have a command to send email everyMinute for a user. It is working fine if I run the command php artisan activity:update , and if I run php artisan schedule:run , it is also working well and the user received that email just once.

However, I still don't know why the everyMinute function in command not working and I can't receive the email everyMinute.
My Kernal:

protected $commands = [
    Commands\Inspire::class,
    Commands\ActivityDateChecker::class,
    Commands\Test::class
];


protected function schedule(Schedule $schedule)
{
    $schedule->command('activity:update')
             ->everyMinute();
}<br>

I already read some articles mentioned that need to add the schedule command (* * * * * /usr/bin/php /var/www/html/Projectname/artisan schedule:run >> /dev/null 2>&1) running in crontab,

When I run crontab -e
I got:

no crontab for ec2-user - using an empty one
~
~
~
~
~
~
~
~
~
"/tmp/crontab.p7jciw" 0L, 0C

So I found another way to open crontab, by using: sudo vi /etc/crontab
I got this screen:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               
~                                                                               


and when I put this line (* * * * * /usr/bin/php /var/www/html/Projectname/artisan schedule:run >> /dev/null 2>&1) (copy/paste) and I tried to save it, I couldn't save it because of the permission of username

First, Do I need to embed it in crontab to make the command working everyMinute? and where is the missing part in my code that make it not trigger auto everyMinute?

Second: How I can make my command running everyMinute, while it is working manually, just the automated part not working.


Solution

  • If you run it manually it'll only run once. When you put the command in your cron tab, the cronjob will run every minute, and therefor your email command will trigger every minute.

    What the cron does is run schedule:run every minute, the same thing you do manually.

    So add the line to your crontab and you'll receive an email every minute