1

I am having an issue implementating a Cron Job for my Laravel application on a Apache/CPanel shared server. I have a task command that works fine whenever i run it from the command line and also works fine when called daily but it is not running on a "Once a day" schedule.

The Cron Job bellow works perfectly when called each minute only if the Laravel command is also called everyMinute() as shown:

Cron call

php -d register_argc_argv=On /home/path/domain.com/artisan schedule:run > /dev/null 2>&1

Server schedule * * * * *

Laravel command:

$schedule->command('alert:dailly')->everyMinute();

The problem is that the command that i really want to work is once a day, and that doesn't work. The same command that once worked fine each minutes its just not called when once a day, as follows:

Cron call

php -d register_argc_argv=On /home/path/domain.com/artisan schedule:run > /dev/null 2>&1

Server schedule 0 0 * * *

Laravel command:

$schedule->command('alert:dailly')->cron('0 0 * * * *');

or

$schedule->command('alert:dailly')->daily();

Is there anything wrong?

Thank you!

Luiz Wynne
  • 460
  • 3
  • 10
  • 28
  • 3
    `dailly` - Is that a typo, or does your command name actually have 2 l's? That could very easily explain why running `php artisan alert:daily` works but `$schedule->command('alert:dailly')` does not :) – Tim Lewis Nov 08 '19 at 21:09
  • make sure the cronjob that calls the scheduler is running every minute ... if you don't do this it will not be running at the exact minute something needs to be ran and it wont get ran ... how laravel decides when something falls on the day may not be when the cron runs if not running every minute – lagbox Nov 08 '19 at 21:33
  • Yes, the scheduler run every minute and not, the mispelling is not an issue. I know the 'dailly' word is wrong but that is not the cause of the problem, because i call the laravel method right – Luiz Wynne Nov 09 '19 at 12:49

2 Answers2

2

You don't need to change the server schedule on your daily call.

There should be one artisan cron on your server like so:

* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

This cron will be called every 1 minute and when it does, Laravel will evaluate your scheduled tasks.

Later on your code use the daily:

$schedule->command('cmd-name:op')->daily();

Daily will run the task every day at midnight.

For other schedule frequencies check the docs

Elad
  • 891
  • 5
  • 15
0

I had a similar issue and never found out the real reason why. I solved it by forcing the daily execution at a particular time and in the background. Your code would then be (assuming you like to run it at 1:30 in the morning):

$schedule->command('alert:dailly')->dailyAt('01:30')->runInBackground();

What I also noticed is that I had to run commands at quarters of an hour. so (hour):00, (hour):15, (hour):30 and (hour):45 worked out, (hour):03 for example not.

It could be a hosting issue (in my case A2hosting.com, shared) but the knowledgebase doesn't make me any of the wiser. It's worth the try. Run it out at a time you can monitor the process with the Linux command top -ac so you can see if the command is actually executed.

If this works out for you, don't ask me why. I had to find this solution for myself through trial and error.

I'd like to point out, as explained in this PHP 'bug' report that your cron call never reaches the register_argc_argv=On argument. Try to run it without any arguments.

php /home/path/domain.com/artisan schedule:run > /dev/null 2>&1

Unless differently advised by your hosting service.

Dimitri Mostrey
  • 2,302
  • 1
  • 12
  • 11