Run an Laravel Schedule every minute?

Asked

Viewed 1,609 times

4

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

I did the following in the Kernel App:

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

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

To create the cron executed:

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

But nothing happens. I need to do something else to make it work?

  • If you do not create cronjob (linux) or schedtask (windows), it will never run. See the documentation to understand how it works: https://laravel.com/docs/5.3/scheduling

  • the only thing the documentation talks about is to run * * * * * php /path/to/Artisan Schedule:run >> /dev/null 2>&1, I did this but nothing :/

  • I will specify

  • @Danielomine is specified in the question

  • @Danielomine It’s the first time I try to mess with Schedule, so I’m well lost kkkk

1 answer

2


Log into the server with user permissions to create schedules. For this you can use SSH (console) or use resources of management tools of websites like Plesk, Cpnel, etc.

Via console (SSH), after logging in, run

crontab -e

This will open the logged-in user’s scheduling 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 text editor.

Save the file change

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

The letters wq mean "wRite" and "quit".

To make sure it’s even scheduled, run

crontab -l

A schedule list will be displayed.

However, this still does not guarantee much. If there is an error in the file path or the file script fails, you will not be able to 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. Invent whatever is convenient for testing.

Using a manager like Plesk, Cpanel, etc, you 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

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

In Cpanel it is similar to this. The images are illustrative.

  • Unfortunately I am obliged to develop this project in Windows, but using its logic I created a bat to run a command that runs Schedule, the only problem is that every 5 minutes opens a beautiful command prompt :'), but I will run in hand to develop and create the cron on the server when sending to production, worth :)

  • 1

    just add the command to run in the background.....

  • @Felipepaetzold has trouble-free running in windows as well. Another time asked the answer.

Browser other questions tagged

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