Najpierw w cronie musisz dopisac linike ze sciezka do aplikacji ja mam /var/www/stronka
* * * * * cd /var/www/stronka && php artisan schedule:run >> /dev/null 2>&1
Dzieki temu twoj cron co mienute wykona polecenei ktore sprawdza czy w kolejce w laraverze cos jest do wykoniania. Nastepnie musisz stworzyc swoja komende w larvie wiec idziesz do katalogu oczywiscie w consoli to musisz zrobic
cd /var/www/stronka
i wpisz:
php artisan make:command CheckDate
to tylko propozycja nazwa CheckDate moze byc dowolna taka jak uznasz. Dzieki temu zrobi ci sie plik w katalogu /var/www/stronka/app/Console/Commands i tam ma byc plik CheckDate jak jest ortworz go
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Code;
class CheckDate extends Command
{
/**
* The name and signature of the console command.
*
*
* @var string
*/
protected $signature = 'check:date';
/**
* The console command description.
*
* @var string
*/
protected $description = 'checking date';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
echo "Hey to ja \n";
}
}
teraz otworz w katalogu /var/www/stronka/app/Console plik Kernel.php
i tam zrob tak:
<?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\CheckDate',
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('check:date')->everyFiveMinutes();
// inne przyklady nizej
//$schedule->command('check:date')->daily();
//$schedule->command('check:date')->monthlyOn(1, '00:00');
//$schedule->command('check:date')->weeklyOn(1, '00:00');
//$schedule->command('check:date')->hourly();
//$schedule->command('check:date')->dailyAt('03:00');
//$schedule->command('check:date')->cron('0 */2 * * *'); // every 2 hours
//$schedule->command('check:date')->daily();
//$schedule->command('check:date')->hourly();
//$schedule->command('check:date')->dailyAt('07:00');
//$schedule->command('check:date')->dailyAt('03:00'); */
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
dzieki temu mozesz wejsc na katalog /var/www/stronka i wpisz swoja komende php artisan check:date i wyswietli ci sie: Hey to ja. Tam wlasnie umieszczsz funkcje do laczenia zbaza i robisz to co potrzebujesz. Pozniej kazdy cron dodajesz juz tylko do laravela. 10 sekund pracy :) i gotowe. Nizej masz strone z ustawieniami jak portzebujesz by sie cos wykonywalo co iles czasu to sprawdz sobei tu: https://crontab.guru/
Gdybys potrzebowal w przyszlosci uzyc paramteru to robisz tak:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Code;
class CheckDate extends Command
{
/**
* The name and signature of the console command.
*
*
* @var string
*/
protected $signature = 'check:date {--param1=} {--param2=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'checking date';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
echo $this->option('param1');
echo $this->option('param2');
}
}
a wywolujez to tak:
php artisan check:date --param1 mojedane --param2 innedane