Wednesday, 5 March 2025

Laravel 12 Scheduling Hello world!

Many tasks require to be run every hour or day etc.  On the server this is done using a cron.

Laravel provides an easy way to create scheduled events and (importantly) how to test them before they reach the cron stage on the server.

Below is a "Hello world" example, which should offer some confidence and inspiration.

At the end of routes/console.php add the following lines:

Schedule::call(function () {

    echo "Hello World";

})->everyTwoSeconds();

Now run the command:

php artisan schedule:list

The command helpfully tells where the scheduling is going to take place.

Now let's run the task using the command:

php artisan schedule:work

After a short delay, you will see "Hello world" being outputted every 2 seconds as we stipulated.

Now you can see all sorts of options.

No comments:

Post a Comment