As you are using Laravel, you can write that your function within the method protected function schedule(Schedule $schedule)
class App\Console\Kernel
.
The example of what your role would look like follows below:
protected function schedule(Schedule $schedule) {
$schedule->call(function () {
$palpites = Listadepalpites::all();
$data = date('Y-m-d');
$data1 = date('Y-m-d', strtotime($data. ' -3 days'));
// Código segue...
})->dailyAt('06:00');
}
For your cronjob code, you also need to put the following line in your cronjob:
* * * * * php /caminho-para-o-projeto/artisan schedule:run >> /dev/null 2>&1
EDIT
You can also use the classes Command
to schedule the Js (particularly, you can reuse your code better this way).
Follow an example:
Open the CMD in your project folder and type php artisan make:command AtualizaPontuacao
.
Laravel will generate a file called AtualizaPontuacao.php
inside app\Console\Commands
. Inside it, there will be an attribute called protected $signature
. In this attribute, you will tell which signature of your command. For example: atualiza:pontuacao
.
Within the method public function handle()
, you can write all your code.
For your command to work, go back to class App\Console\Kernel
and add inside the attribute protected $commands = [ ... ]
the name of your Command class: App\Console\Command\AtualizaPontuacao::class
Now, change the code inside the method protected function schedule(Schedule $schedule)
for the example below:
$schedule->command('atualiza:pontuacao')->dailyAt('06:00');
Ready! Now, your code will run both in cronjob and manually too, through the command php artisan atualiza:pontuacao
.
For more cronjob and Command time and date settings, please visit the Laravel documentation:
Cronjobs: https://laravel.com/docs/5.5/scheduling
Artisan Commands: https://laravel.com/docs/5.5/artisan#writing-Commands
Well you run a function in Cron exactly how a function runs in a file in the common browser... So you have a file with a function called
getAllUsers($parametros);
then, at the end of the file you set in Cron, you putgetAllUsers($parametros)
and that’s it... Good luck– Diego Ananias
My doubt is that a Function is always executed with a call. and in the script I have to write the Function or as?
– Dan Even
Cron for me is closely linked to the command line. Run on the command line your program and it will show the same behavior in cron
– Jefferson Quesado