I have a command scheduled in the Laravel 5.4 scheduler and would like to start the Laravel cron on Mac OS X El Capitan.
app/Console/Kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
'App\Console\Commands\GetToken'
];
protected function schedule(Schedule $schedule) {
$schedule->command('gettoken')->everyMinute();
}
protected function commands() {
require base_path('routes/console.php');
}
}
My GetToken.php makes an API call and then a DB change. I believe that this is working properly, as I can run the task directly from the cli using:
php /path/to/project/artisan schedule:run 1>> /dev/null 2>&1
To edit my cron file I use:
env EDITOR=nano crontab -e
I then add:
* * * * * php /path/to/project/artisan schedule:run >> /dev/null 2>&1
I save with ctrl+o and exit with ctrl+x.
Re-editing the file shows that the changes have saved.
Running crontab -l shows the text that I entered into the crontab file.
My cron never runs. I can only get it to run once by running manually using the command I mentioned above.
Not directly answering your question, but proposing another solution:
If you want to set up cron jobs for your development environment, it's best to use Homestead, for its Linux standards compliance.
For small projects that i develop directly inside macOS, i run the following command inside the project root (in a separate terminal tab) to have my jobs run every minute:
while true; do php artisan schedule:run; sleep 60; done
This helps to make sure, the cron jobs are only run while i'm developing. When i'm done, i Ctrl+C that command and can be sure nothing unexpected happens while i'm not watching.
Plus it gives me the freedom to adjust the interval, by simple choosing another number of seconds for the sleep command. This can save time when developing.
Update Laravel 8.x
Laravel now offers the above as a single artisan command:
php artisan schedule:work
Related
I have a Laravel 5.5 installation and am trying to get the scheduler to run. I have the following cron job running and it is working fine.
* * * * * php /var/www/html/project1/artisan schedule:run >> /var/www/html/project1/schedule.log
The cron job is outputting to a log file and I get the same result in cron as I get if I run artisan schedule:run from the command line. The result is always the same and says no scheduled commands are ready to run. The problem doesn't appear to be with cron as the job runs and I get output.
I configured the simple artisan inspire command to run in the Laravel schedule function. I can run the inspire command on the command line with php artisan inspire and it works fine. I understand all the posts about timezone but it is not a timezone issue, the command simply doesn't run ever. I am also not using withoutOverlapping(), so that is not my problem. So I have a cron job running every minute to run the laravel schedule and it does output to my log, which indicates that cron and Laravel are communicating. I think the problem must be that Laravel doesn't think there is a command that needs to be executed. I don't know how to debug this any further so any help would be greatly appreciated.
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
//
];
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')
->everyMinute();
}
protected function commands()
{
require base_path('routes/console.php');
}
}
I'm using Laravel 5.4 on a local WAMP Server. I wanted to perform a Task Scheduling but I'm not sure if I really understood how this works.
I created a command cronEmail and in the handle() function added code where I would get an Email.
In Kernel.php I added this:
protected $commands = [
'App\Console\Commands\cronEmail'
];
...
protected function schedule(Schedule $schedule)
{
$schedule->command('send:email')
->everyMinute();
}
So basically I want to get an email every minute. But how do I start this?
I tried entering:
php artisan schedule:run >> /dev/null 2>&1
or even
php C:\wamp64\www\seo-parser\artisan schedule:run >> /dev/null 2>&1
on my cmd but I always get:
The system cannot find the path specified.
If i enter php artisan schedule:run it will actually send an email but only once.
Did I understand the whole concept wrong? How do I do this properly?
Thank you,
Patrick
As stated in the official Laravel documentation you need to add the following line to your crontab.
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
You do this by typing "crontab -e" in the console. Paste the above line and replace the "/path-to-your-project" with the path to your project.
This results in a cronjob wich calls the "php artisan schedule:run" command every minute.
This requires you to run Linux though. If you need an alternative to crontab when running Windows you can start by reading here.
I created a command in laravel "update:date" and "php artisan list" successfully lists it. I am able to execute it using "php artisan update:date"
Kernel.php:
protected $commands = [
'Snuba\Console\Commands\Inspire',
'Snuba\Console\Commands\UpdateDate'
];
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')
->hourly();
$schedule->command('update:date')
->everyMinute();
}
I configured it to run every minute as given above. Do I need to configure anything else ? I think laravel should automatically register it as cron task on ubuntu server.
I think laravel should automatically register it as cron task on
ubuntu server.
No, you have to start it like:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
Starting The Scheduler
When using the scheduler, you only need to add the following Cron entry to your server. If you do not know how to add Cron entries to your server, consider using a service such as Laravel Forge which can manage the Cron entries for you
Reference
Another Answer
I just noticed:
Call to undefined method Illuminate\Console\Scheduling\Event::everyMinute()
I followed the documentation and thought this is a valid method call. I found that everyMinute() is no more valid in laravel 5+. So, I used cron expression instead.
Laravel's command scheduler allows you to fluently and expressively define your command schedule within Laravel itself. For more go through following link laravel task scheduling
If you have access to SSH
run command crontab -e
insert this line:
php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
which will run all your specified crons as there specified times.
If you have cpanel then under crontab add the above line on command
If ->everyMinute does not exist, please use ->cron('*/1 * * * * *') to set every minute. Or override class laravel.com/api/5.x/Illuminate/Console/Scheduling/Event.html
If cron job not working, run command crontab -e and add this line without single quote
'* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1'
After do this I think it works perfectly.
I have a task I'm trying to run every day. In my Kernel.php I have the following command:
$schedule->command('emailProgram')->daily()->timezone('US/Central');
I'm my crontab I have:
* * * * * php /var/www/html/appname/artisan schedule:run >> /dev/null 2>&1
So, When I run php artisan schedule:run, or run it directly with php artisan emailProgram it runs as expected. But, its not running on its own using daily()/dailyAt() or otherwise. Lastly, if I remove daily() from the command in the kernel.php file:
$schedule->command('emailProgram')->timezone('US/Central'); it's running every minute, so its like there is some disconnect with the Laravel task helpers. This is my first time setting up Cron, and task management with Laravel so maybe I'm overlooking something simple. Any help would be really appreciated, thanks.
My laravel version is 5.0.28, I build on cloud9, and I added this command to my cron:
#!/bin/bash
PATH=/usr/bin
* * * * * php /home/ubuntu/workspace/app/artisan scheduled:run 1>> /dev/null 2>&1
I added this code on my Kernel.php. I referenced this site: https://laravel-news.com/2014/11/laravel-5-scheduler/
<?php namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Http\Controllers\ApiController;
class Kernel extends ConsoleKernel {
protected $commands = [
'App\Console\Commands\Inspire',
];
protected function schedule(Schedule $schedule)
{
$schedule->call('ApiController#test_job')->hourly();
}
}
I waited and it still didn't work, so I tried to use the command php artisan schedule:run, and I got: No scheduled commands are ready to run.
I searched and found this answer: Laravel 5 "Class does not exist" when using the scheduler
So I modified my code. Also, this code had no specified time, so I modified my cron to specify a time, but it still doesn't work. I have no more ideas. please help. Thanks.
code
$schedule->call(join('#', [ApiController::class, 'test_job']));
cron
0 0,3,6,9,12,15,18,21 * * * php /home/ubuntu/workspace/app/artisan schedule:run 1>> /dev/null 2>&1
30 1,4,7,10,13,16,19,22 * * * php /home/ubuntu/workspace/app/artisan schedule:run 1>> /dev/null 2>&1
first to Test if cron is running in your server or localhost type:
> sudo service cron status
if not installed:
> sudo apt-get install cron
to enable laravel's scheduler:
> crontab -e
and you can select an editor if not vim opens directly. Be sure to enter there this line at the bottom:
* * * * * php /path_from_root_to_laravel_proj_folder/artisan schedule:run 1>> /dev/null 2>&1
to Test if you have setup inside laravel the scheduler right, run this from your projects folder:
>php artisan schedule:run
this should execute the tasks and tell you what is doing.
Laravel scheduler works with commands, not with controller methods:
create command:
php artisan make:command PurchasePodcast
edit command:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class PurchasePodcast extends Command
{
protected $name = 'purchase:podcast';
public function fire()
{
// do stuff here
}
}
add command to Console\Kernel.php:
protected $commands = [
'App\Console\Commands\PurchasePodcast',
];
use command in scheduler:
$schedule->command('purchase:podcast')->hourly();
In my case, the scheduler actually run but encountered an error because of lower php version, beside artisan path (which is your project folder), I had to set the php path as below:
* * * * * /path_to_php_folder/bin/php /path_from_root_to_laravel_proj_folder/artisan schedule:run 1>> /dev/null 2>&1
There are several aspects to find the cause:
First, check whether the 'timezone' in config/app.php is set properly. Laravel will reset the timezone even though you already configured it in php.ini.
Secondly, check that crontab is working as expected. When you get the message "No schedule to be ready", it means your crontab is running and can detect the php and artisan command.
In order to complete #limonte's answer the create Console Command is the following:
php artisan make:console CampaignsCollect --command=campaigns:collect
Reference here: link