Run a Laravel Schedule every minute?

4

I'm using Laravel 5.3 and would like to run a task every minute, within the task I'll put some checks, the question is to make it run every minute.

I did the following in App \ Kernel:

protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();

        $schedule->call(function () {
            return redirect('/contas');
        })->everyMinute();
    }

To create cron executei:

php /app/console/Kernel.php schedule:run >> /dev/null 2>&1

But nothing happens. Do I need to do anything else to work?

    
asked by anonymous 23.11.2016 / 18:07

1 answer

2

Log in to the server with a user with permissions to create schedules. For this you can use SSH (console) or use features of site management tools such as Plesk, CPnel, etc.

Via console (SSH), after logging in, run

crontab -e

This will open the logged in user's schedules file.

For your case, you should create a schedule that runs every minute. Add something like this:

1 * * * * php /caminho/completo/do/script.php

To start editing, press the "I" key. It depends on the linux distribution and the text editor.

Save the file change

(pressione) ESC
(digite) :wq
(pressione) ENTER

The letters wq mean " w rite" and " q uit".

To make sure it is actually scheduled, run

crontab -l

A list of schedules will be displayed.

However, this still does not guarantee much. If there are any errors in the file path or the file script fails, you will not know if there is no log, monitoring, etc.

To see if the path is correct, run on the console

php /caminho/completo/do/script.php

And see what happens. To test, put a echo time(); at the end or put a mail() to send an email, file_put_contents() to create a file, anyway. Make up what is convenient for testing.

Using a manager such as Plesk, CPanel, etc., will have a page to create schedules in a friendlier way without using a console.

Usually just type the same command

1 * * * * php /caminho/completo/do/script.php

CPanel is similar to this. The images are illustrative.

    
23.11.2016 / 19:09