Reputation: 273
I will like to know how to schedule a cron job to run everyday at 00:01.
I have created JOB
in App/Jobs
folder
<?php
namespace App\Jobs;
use App\Models\Result;
use App\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling;
use DB;
set_time_limit(0);
class UpdateActive extends Job implements SelfHandling
{
public static function ActiveUpdate()
{
Result::update(['draw_id' => 1,
'isactive' => 0
]);
}
public static function downGrade()
{
try {
UserRole::update(['permission' => 1,
'isactive' => 2
]);
} catch (QueryException $e ) {
//handle error
}
}
public static function handle()
{
self::ActiveUpdate();
self::downGrade();
}
}
in App/Console/Kernel.php I have added this link to the schedule
method
protected function schedule(Schedule $schedule)
{
/*$schedule->command('inspire')
->hourly(); */
$schedule->call(function () {
$check_draw = \App\Jobs\UpdateActive::ActiveUpdate();
})->everyMinute();
}
Please note I have used everyMinute for test purpose
In crontab -e
I then added
* * * * * php /home/vagrant/Code/projects/artisan schedule:run 1>> /dev/null 2>&1
but the schedule doesn't seem to run i think because when i check my results
table the isactive
field hasn't changed.
I am wondering where I am going wrong please. If anyone has done this in L5. What am I missing?
Upvotes: 6
Views: 11480
Reputation: 1
php artisan make:command PushNotification
make changes in app->console->kernal.php write the code below,
protected function schedule(Schedule $schedule)
{
$schedule->command('log:sendPush')->everyMinute();
}
crontab -e (in Terminal)
press i to insert in terminal
edit below mentioned line
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
* * * * * cd /var/www/html/api && php artisan schedule:run >> /dev/null 2>&1
press escape then write :wq! and press enter
this is the way to edit in terminal
Then write your code in PushNotification created job in public function handle
Enjoy Laravel Cron Job.
Upvotes: 0
Reputation: 1
namespace App\Console\Commands;
use App\Models\Users; use DB;
use Illuminate\Console\Command;
class CronJob extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'command:name';
protected $new = 'cronjob';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$users = DB::table('users')
->where([
['end_date', '>=',date("Y-m-d", strtotime('+ 1 day'))],//some condition
['start_date', '<=',date("Y-m-d", strtotime('+ 1 day'))],//some condition
])->get();
foreach ($users as $values ){
//Do Something
}
}
Upvotes: 0
Reputation: 12127
I will like to know how to schedule a cron job to run everyday at 00:01.
So you want it to run daily at 00:01?
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
$check_draw = \App\Jobs\UpdateActive::ActiveUpdate();
})->dailyAt("00:01");
}
This is how I would do it:
The command:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class UpdateActiveCommand extends Command
{
protected $signature = 'update-active';
protected $description = 'Update something?';
public function handle()
{
try {
$this->comment("Update active...");
$this->updateActive();
$this->comment("Downgrade...");
$this->downGrade();
$this->info("Done!");
} catch (QueryException $e ) {
$this->error($e->getMessage());
}
}
private function updateActive()
{
Result::update([
'draw_id' => 1,
'isactive' => 0,
]);
}
private function downGrade()
{
UserRole::update([
'permission' => 1,
'isactive' => 2,
]);
}
}
The scheduler:
<?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\UpdateActiveCommand::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('update-active')
->dailyAt('00:01')
->sendOutputTo(storage_path('logs/update-active.log'))
->emailOutputTo('[email protected]');
}
}
If you did it this way you could also run it from the command line with php artisan update-active
and see the output.
Upvotes: 9