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
Related
I create a schedule to send email everyday, but for testing if it works i make it run every minute.
here is my kernel.php
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
Commands\SendExpiredReminder::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();
$schedule->command('reminder:send')
->everyMinute();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
it run every minute but reminder:send only called first time i run the schedule with php /var/www/html/artisan schedule:run >> /dev/null 2>&1
when i run php /var/www/html/artisan schedule:list it show that next due is next minute indicated the scheduler is running. What is going wrong?
your crontab should be to your laravel project folder, not www folder
* * * * * cd /var/www/html/artisan && php artisan schedule:run >> /dev/null 2>&1
note the project folder
What I'm intending to do is to make a task scheduling for weekly, which is to copy all of id from table route_schedule and insert into table route_schedule_details as FK, which then will insert the date of the weeks. This is how the route_schedule_details schema:
Schema::create('route_scheduler_details', function (Blueprint $table) {
$table->id();
$table->dateTime('schedule_date')->nullable();
$table->unsignedBigInteger('route_scheduler_mstr_id')->nullable()->index('FK_route_scheduler_details_route_scheduler_mstr');
$table->foreign(['route_scheduler_mstr_id'], 'FK_route_scheduler_details_route_scheduler_mstr')->references(['id'])->on('route_scheduler_mstr')->onDelete('cascade');
});
I never used task scheduling before so I'm little bit under-knowledged here. Read in Laravel docs, I have to add the schedule in App/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$data = [];
$copies = RouteSchedulerMSTR::select('id')->get();
foreach($copies as $copy){
//I'm not sure what to do in here
}
})->weekly(1, '1:00');
}
Firstly, you need to create command because above your code can't test work or not :
php artisan make:command RouteSchedulerWeekly
<?php
namespace App\Console\Commands;
use App\Models\RouteSchedulerMSTR;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class RouteSchedulerWeekly extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'routeschedule:weekly';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Run route scheduler weekly';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
$copies = RouteSchedulerMSTR::select('id')->get();
foreach($copies as $copy){
DB::table('route_scheduler_details')->insert([
'data' => now(),
'route_scheduler_mstr_id' => $copy
]);
$this->info('Route scheduler inserting...');
}
$this->info('Done!');
}
}
Then you can test your code insert or not :
php artisan routeschedule:weekly
if you wanted function is correct, you can run by cronjob with 2 options
Direct run command with cron
crontab -e
and add below script to crontab file on your server:
0 1 * * 6 cd /var/www/your-project && php artisan routeschedule:weekly >> /dev/null 2>&1
You can test crontab script here
In Windows use Task Scheduler
OR
Run with schedule:run
Add your command in Kernel
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('routeschedule:weekly')->weekly(1, '1:00');
}
}
and add below script to crontab file on your server:
* * * * * cd /var/www/your-project && php artisan schedule:run >> /dev/null 2>&1
here * * * * * is every minute
Conclusion: I recommend option 1 that will run once in a week on your server. The option 2 also run once in a week your command but schedule:run running every minute
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.
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 setup a scheduled task to fire an artisan command like below:
*/
protected $commands = [
'App\Console\Commands\GoogleAdsCronJob',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('googleads:cron')
->everyFiveMinutes()
->appendOutputTo(self::LOG_PATH);
}
Then the path in ubuntu server /var/www/html/myproject I ran the command:
* * * * * php /var/www/html/my-prject/artisan schedule:run >> /dev/null 2>&1
eventually nothing happened ? Logs are not saying anything!