Backend Development 6 min read

Creating and Scheduling Laravel Artisan Commands for Daily Automated Tasks

This guide walks through setting up a Linux‑Nginx‑MySQL‑PHP environment, generating a Laravel Artisan command, configuring the Kernel schedule, and adding the necessary cron entry to run the task automatically each night at the specified time.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Creating and Scheduling Laravel Artisan Commands for Daily Automated Tasks

The article explains how to prepare a Linux server with Nginx, MySQL, and PHP, install the Laravel framework, and implement a daily scheduled operation that runs at 1 am.

Step 1 – Create the command: Use Laravel's Artisan tool to generate a new command named Test with the command php artisan make:command Test . This creates a file under app/Console/Commands and registers it in the console kernel.

// php artisan make:command Test

Step 2 – Edit the generated command file: The Test class extends Command , defines the $signature (e.g., 'test:data' ) and $description , injects a CalculateDataService via the constructor, and implements the handle() method to call the service and output a success message.

<?php namespace App\Console\Commands; use App\Services\CalculateDataService; use Illuminate\Console\Command; class Test extends Command { protected $signature = 'test:data'; protected $description = 'test data'; protected $service; public function __construct(CalculateDataService $service) { parent::__construct(); $this->service = $service; } public function handle() { try { $this->service->calculateData(); } catch (\Exception $e) { $this->error($e->getMessage()); } $this->line('calculate Data Success!'); } }

Step 3 – Register the command and define the schedule in Kernel.php : Add the command class to the $commands array, then use the schedule() method to chain the command execution with after() hooks, ensuring tasks run in the desired order.

<?php class Kernel extends ConsoleKernel { protected $commands = [ Test::class, CalculateData::class, UpdateOffset::class, ]; protected function schedule(Schedule $schedule) { $schedule->command('iot:sync Flow') ->after(function () { Artisan::call('Test:data'); }) ->after(function () { Artisan::call('calculate:data'); }); } protected function commands() { $this->load(__DIR__.'/Commands'); require base_path('routes/console.php'); } }

The schedule() method can be customized with various hooks ( after , before , etc.) to control execution order and timing.

Step 4 – Activate the scheduler with cron: View existing cron jobs using crontab -l , edit the crontab with crontab -e , and add an entry such as 0 1 * * * php /path/to/your/project/artisan schedule:run >> /dev/null 2>&1 to run the Laravel scheduler each night.

crontab -l

crontab -e

After adding the entry, the Laravel scheduler will invoke the defined commands at the configured times, completing the automated workflow.

The article concludes by encouraging readers to refer to the official Laravel documentation for further scheduling options.

backendSchedulingphpCronLaravelArtisanCommand
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.