1

In my laravel project, I have this file in my tests directory:

tom_test.php:

    <?php
        error_log("hello world");

I want to schedule this script to run every 'x' amount of time. I've seen this question from cyber8200. Unfortunately, I'm unsure how to make this test script to run.

Do I have to convert the test file to a class instead?

I would appreciate it if somebody would explicitly state what code needs to be added to the app/Console/Kernel.php file. I suspect it should resemble some of the code in the cyber8200 question

UPDATE

Thanks to Tim Lewis' comment, I've managed to get the cron job running (I think).

Unfortunately, I cannot see the "hello world" message being logged to the console.

Here is what I've done

I added a command as follows:

    public function handle()
    {
        error_log("hello");
//        echo base_path();
        exit;
        include base_path().'/tests/Browser/tom_test.php';
    }

and this schedule function to the kernel:

protected function schedule(Schedule $schedule)
{
     $schedule->command('tom_test')
              ->everyMinute();
}

This is the result I get: enter image description here

The job seems to be quitting after one run, and no message is logged to the console.

Tomk07
  • 113
  • 2
  • 12
  • 2
    Laravel's Kernel can process Commands, so you'll need to make one (https://laravel.com/docs/8.x/artisan#generating-commands) then call it via the Scheduler (https://laravel.com/docs/8.x/scheduling#scheduling-artisan-commands), then setup a CRON task locally to run it (https://laravel.com/docs/8.x/scheduling#running-the-scheduler). Note: These docs are for Laravel 8, but it's a similar approach for Laravel 5. – Tim Lewis Apr 14 '21 at 16:24
  • Thanks, I believe I'm now able to get the command to run, but I can't see any output. I will update the question – Tomk07 Apr 14 '21 at 17:59
  • 1
    You should be able to do a simple `$this->error('Hello')` :) See the section on writing output: https://laravel.com/docs/8.x/artisan#writing-output. Also, if you need to easily test the command, don't run `php artisan scheduler:run`, just run the command directly via `php artisan {command:signature}` (replacing `{}` with your actual command). It looks like the output is being supressed by `> NUL 2 > &1` – Tim Lewis Apr 14 '21 at 18:35

0 Answers0