How to run a Function in cron job?

Asked

Viewed 788 times

0

I wonder how and what is the best way to do a cron job, if I write it as a direct Function in cron? I’d like to do it every day at 6:00.

public function atualizarpontuacao(){
       $palpites = Listadepalpites::all();
       $data = date('Y-m-d');
       $data1 = date('Y-m-d', strtotime($data. ' -3 days'));
         foreach($palpites as $p){
           $palpitea = $p->palpitea;
           $palpiteb = $p->palpiteb;
           $id       =  $p->id;
           $id_c     = $p->id_c;
           $id_u     = $p->id_u;
          $confrontos = Listadejogos::Find($id_c);
           $scorea   = $confrontos->scoretimea;
           $scoreb   =  $confrontos->scoretimeb;
           $pontuacao =3;

           DB::update("update palpite set pontuacao='$pontuacao' where (id='$id' and diadojogo > '$data1') ");

         }
  • 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 put getAllUsers($parametros) and that’s it... Good luck

  • My doubt is that a Function is always executed with a call. and in the script I have to write the Function or as?

  • 1

    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

1 answer

0


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:

  1. Open the CMD in your project folder and type php artisan make:command AtualizaPontuacao.

  2. 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.

  3. Within the method public function handle(), you can write all your code.

  4. 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

  5. Now, change the code inside the method protected function schedule(Schedule $schedule) for the example below:

    $schedule->command('atualiza:pontuacao')->dailyAt('06:00');

  6. 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.