I'm trying to run command every minute with scheduler but scheduler:run prints No scheduled commands are ready to run. message. As I understand, I don't need to set cronjob if I want to test shcedulder by run command. Any suggestions how to solve this issue are appreciated
My command:
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'print:log';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Print in log';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
info('message from command');
}
Kernel.php
protected $commands = [
'App\Console\Commands\PrintLog',
];
protected function schedule(Schedule $schedule)
{
$schedule->command('print:log')->everyMinute();
}
Can you try as?
protected $commands = [
Commands\PrintLog::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('print:log')->everyMinute();
}
You misunderstand. Read docs here.
schedule:run command is for production - you must use it with cronjob on server
schedule:work command is for local development and testing. Run this command in terminal window and wait one minute - your scheduled job will be dispatched.
Related
please help me i'm trying to make a cron job on my server to send email to user
i created a command like that :
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'contract:end';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Send an email to user about the end of a contract';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$contracts = Contract::where('end_date', Carbon::parse(today())->toDateString())->get();
foreach ($contracts as $contract) {
Mail::to(Setting::first()->value('email'))->send(new ContractEmail($contract));
}
}
and in my kernel.php i added the following code :
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
//
Commands\ContractEnd::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('contract:end')
->everyMinute();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
and on my server i run the following command to run the cron job :
cd /archive/artisan && schedule:run >> /dev/null 2>&1
but it didn't send any email , can anyone help me as it's my first time using it ?
Because your command in your server is wrong you need to specify the correct path which will contain/home/your username/your website folder/artisan like this
/usr/local/bin/php /home/your user name which you can get in from job page/archive/artisan schedule:run 1>> /dev/null 2>&1
I have a simple set up a command for Scheduling that is firing on a cron call
The artisan command works fine yet nothing happens when Scheduling
The Schedule
protected function schedule(Schedule $schedule)
{
$schedule->command('prices:pricing')
->everyMinute();
}
The Task
protected $signature = 'prices:pricing';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Gets Pricing data and stores in DB';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
Log::debug('An informational message.');
}
}
The cron
* * * * * www-data cd /var/www/api && php artisan schedule:run >> /dev/null 2>&1
Also I have tried with running
php artisan queue:listen
i am working on laravel crone job it work only one time my code execute only one time i am a beginer
protected function schedule(Schedule $schedule)
{
$schedule->command('list:users')->everyMinute();
}
class ListUsers extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'list:users';
/**
* The console command description.
*
* #var string
*/
protected $description = 'corn job command';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$totalUsers = \DB::table('users')->whereMonth('created_at','04')->count();
Mail::to('qasim#gmail.com')->send(new SendMailable($totalUsers));
}
I am also try this code
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
echo "pakistan\n";
$totalUsers = \DB::table('users')->whereMonth('created_at','04')->count();
Mail::to('qasim#gmail.com')->send(new SendMailable($totalUsers));
echo "pakistan";
})->everyMinute();
}
i want user recieve email after every minute
i recieve mail once but not every minute
I want to run different cronjobs like deleting users, send coupon codes to users, send greeting to users who joined between specified dates and etc. Can I do the same by opening the App\Console\Kernel.php and write the command as below:
protected $commands = [
'\App\Console\Commands\DeleteUsers',
'\App\Console\Commands\SendCouponCode',
'\App\Console\Commands\SendGreetings',
];
protected function schedule(Schedule $schedule)
{
$schedule->command('DeleteUsers:deleteuserscronjob')->everyMinute();
$schedule->command('SendCouponCode:sendcouponcodecronjob')->everyMinute();
$schedule->command('SendGreetings:sendgreetingscronjob')->everyMinute();
}
Also, can someone suggest how to run cronjobs by calling only the methods under controllers and not by commands, as like below?
App\Http\Controllers\MyController1#MyAction1
And,
App\Http\Controllers\MyController2#MyAction2
Using kernel.php to schedule tasks is the correct way to do it according to the Laravel framework.
However, if you want to execute a method in one of your controllers instead of creating a command for it, you can do it like so:
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
MyController::myStaticMethod();
})->daily();
}
For running a schedules task via a command:
protected $commands = [
Commands\MyCommand::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('mycommand:run')->withoutOverlapping();
}
Then in app\Console\Commands\MyCommand.php:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
class MyCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'mycommand:run';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Description of my command';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
// PUT YOUR TASK TO BE RUN HERE
}
}
I'm using Laravel 5.2's task scheduler. I need to be able to pass two options to the scheduler, but I'm not sure how to do this.
Here is what I have in my Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->command('simple_cron --first_option=10')
->everyMinute();
}
And this in my simple_cron command:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Article;
class SimpleCron extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'simple_cron';
/**
* The console command description.
*
* #var string
*/
protected $description = '';
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$firstOption = $this->option('first_option');
}
}
But this gives me the error:
The "--first_option" option does not exist.
What am I doing wrong?
According to the offical documentation the first thing is that your signature needs to have one/two placeholders for parameters:
protected $signature = 'simple_cron {p1} {p2} {--firstoption=5}';
Here we set a default value of 5 for the option firstoption. If you don't want a default value just write {--firstoption=}
To fetch those parameters in your handle method you can do it like that:
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$p1 = $this->argument('p1'); // should be 10
$p2 = $this->argument('p2'); // should be 20
$option1 = $this->option('firstoption'); // should be 99
//
}
You should then be able to call it like this:
$schedule->command('simple_cron 10 20 --firstoption=99')->daily();