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

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?

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

Can't run a task schedule in Laravel 8 using cron job in BlueHost

I am running my web app under Bluehost shared hosting, and I am trying to run a cron job which will call my schedule task in my laravel app. For some reason the scheduler is not being called. These are the commands in Bluehost that I have tried so far:
None of these above commands seems to execute my task scheduler, I have tried calling the command from using artisan command, it works... What do you think this is not working?
Note : This is perfectly working on local server.
/home3/urbanhq6/public_html/new/app/Console/Kernel.php
<?php
namespace App\Console;
use App\Console\Commands\SendReminderEmails;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Console\Commands\SendReminderEmails;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
App\Console\Commands\SendReminderEmails::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('update:record')
->everyMinute();
}
/**
* Register the Closure based commands for the application.
*
* #return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}
/home3/urbanhq6/public_html/new/app/Console/Commands/SendReminderEmails.php
<?php
namespace App\Console\Commands;
use App\Booking;
use Illuminate\Console\Command;
use App\User;
use App\Mail\ReminderEmailDigest;
use Illuminate\Support\Facades\Mail;
use Carbon\Carbon;
use DB;
class SendReminderEmails extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'update:record';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Send email notification to user about reminders.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
DB::table('bookings')->update(["check_out" => date("Y-m-d")]);
}
Cron Job Command
/usr/local/bin/php /home3/urbanhq6/public_html/new/artisan schedule:run >> /dev/null 2>&1

laravel cron not firing but other crons are firing

I have a ubuntu 18.04 setup and running laravel 5.6 and i have set up the default scheduler in crontab -e
* * * * * php /var/www/html/laravel/artisan schedule:run >> /var/www/html/crons.txt 2>&1
and this is not running
my Kernel code
<?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\CompletelyReviewInternship',
'App\Console\Commands\MigrateUsers',
'App\Console\Commands\MigrateUsersTwo',
'App\Console\Commands\MigrateUsersThree',
'App\Console\Commands\MigrateUsersFour',
'App\Console\Commands\MigrateUsersFive',
'App\Console\Commands\MigrateUsersSix',
'App\Console\Commands\MigrateUsersSeven',
'App\Console\Commands\MigrateUsersEight',
'App\Console\Commands\MigrateUsersNine',
'App\Console\Commands\MigrateUsersTen'
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('review:internship')->hourly();
$schedule->command('migrate:users')->dailyAt('17:40');
$schedule->command('migrate:userstwo')->dailyAt('17:40');
$schedule->command('migrate:usersthree')->dailyAt('17:40');
$schedule->command('migrate:usersthree')->dailyAt('17:40');
$schedule->command('migrate:usersfour')->dailyAt('17:40');
$schedule->command('migrate:usersfive')->dailyAt('17:40');
$schedule->command('migrate:userssix')->dailyAt('17:40');
$schedule->command('migrate:usersseven')->dailyAt('17:40');
$schedule->command('migrate:userseight')->dailyAt('17:40');
$schedule->command('migrate:usersnine')->dailyAt('17:40');
$schedule->command('migrate:usersten')->dailyAt('17:40');
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
Please Note :- my commands are firing with php artisan
and i am using nginx as my web server
any advise on this will be advisable
I think there might be multiple reasons of that:
Your application is in maintenance mode. In such case no command will be running unless you use ->evenInMaintenanceMode() for each command
You are using time, but time on your machine is different than what you expect so command will be run at some later point. Make sure time is really valid, or try to change time constraints to ->everyMinute() just to make sure it's not a problem in this case
As a side not - it' not necessary to list all command in $commands property if they are in Commands directory.
if you are using nginx then u have set user www-data in cron tab with super user permission like this
* * * * * sudo -u www-data php /var/www/html/laravel/artisan schedule:run >> /dev/null 2>&1

How to execute a task immediately after adding to Schedule Laravel?

I have next code:
foreach ($tasks as $task) {
$schedule->command(TaskCommand::class, [$task->id])->hourly();
}
I add this tasks dynamically.
How to execute a task immediately after adding to schedule and after that to execute the task every hour?
Foreach should be done in scheduler, look at the code
In app/console/kernel
namespace App\Console;
use DB;
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 = [
//
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
foreach ($tasks as $task) {
$schedule->command(TaskCommand::class, [$task->id]);
}
})->hourly();
}
}
Then you need to switch it on the server by command:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
More: https://laravel.com/docs/5.6/scheduling
Good luck!

Resources