Scheduling in Laravel 5.5 is not properly working - laravel

I have created a command file in commands folder with name "Tournaments" and $signature = 'tournament:start', then updated in Kernal.php file with this command :
$schedule->command('tournament:start')->everyFiveMinutes()->appendOutputTo(storage_path('logs/examplecommand.log'));
Then set the cron job in server like this:
*/5 **** php /directory path/artisan schedule:run 1>> /dev/null 2>&1
But the query is not executed. What could be the problem ? did i miss anything in the scheduling process ?

The proper line to insert in the cronfile is:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
Also, be sure that your command is properly registered: https://laravel.com/docs/5.5/artisan#registering-commands

Since Your Code is Looking Good
So this Must be in App\Console\Kernel.php under schedule function
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('tournament:start')
->everyFiveMinutes()
->appendOutputTo(storage_path('logs/examplecommand.log'));
$schedule->call(function () {
logger()->info(now());
})->cron('* * * * *');
}
So the tournament:start Will run Every Fiveminutes
But as You need to change the cron entry in the server to check for every minutes
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Here is the cron Entry in my server
and here is my schedule function
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('generate:report')
->hourly()
->between('5:00', '23:00');
$schedule->command('sendEmail:deviceOffline')
->everyThirtyMinutes()
->between('5:00', '23:00');
}
so generate:report will run hourly between 5:00 AM and 11::00 PM
And sendEmail:deviceOffline will run run every thiry minutes between 5:00 AM and 11::00 PM

Related

Laravel Schedule Only Exceute Command on first Run

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

Schedule to copy all id of Table A to Table B for weekly and insert a date to Table B Laravel

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

How to let cron decide the time to run laravel schedule daily?

I don't want to set server cron to run every minute (* * * * *) so I set it to #daily
#daily usr/bin/php /home/dss/laravelAppDss/artisan schedule:run >> /home/dss/public_html/example.txt
This is my Kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
'\App\Console\Commands\SinkronDataDosen',
'\App\Console\Commands\SinkronDataMahasiswa',
'\App\Console\Commands\SinkronPotensiDO',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('SinkronDataDosen');
$schedule->command('SinkronDataMahasiswa');
$schedule->command('SinkronPotensiDO');
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
But it doesn't sync the data
This is the output example.txt
Running scheduled command: '/usr/bin/php7.0' 'artisan' SinkronDataDosen > '/dev/null' 2>&1
Running scheduled command: '/usr/bin/php7.0' 'artisan' SinkronDataMahasiswa > '/dev/null' 2>&1
Running scheduled command: '/usr/bin/php7.0' 'artisan' SinkronPotensiDO > '/dev/null' 2>&1
The commands worked if I run each command signature individually so there is nothing wrong with the commands
How do I solve it?

Laravel Scheduling not firing command

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

How to run laravel scheduler in ubuntu server

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!

Resources