{"id":1506,"date":"2024-09-05T17:25:11","date_gmt":"2024-09-05T17:25:11","guid":{"rendered":"https:\/\/debugspot.com\/?p=1506"},"modified":"2026-03-16T11:23:48","modified_gmt":"2026-03-16T11:23:48","slug":"how-to-create-schedule-command-in-laravel-11","status":"publish","type":"post","link":"https:\/\/debugspot.com\/blogs\/how-to-create-schedule-command-in-laravel-11\/","title":{"rendered":"How to Create Schedule Command in Laravel 11"},"content":{"rendered":"<p>Learn how to create and schedule custom commands in Laravel 11 with this detailed guide. This tutorial covers the installation of Laravel 11, creating a custom command, registering and scheduling it, and setting up a cron job on your server. Enhance your Laravel applications by automating tasks with cron jobs.<\/p>\n<p>Laravel 11 brings powerful features and improvements for managing background tasks and automating processes. This comprehensive guide will walk you through creating custom commands, scheduling them, and setting up cron jobs on your server to ensure your Laravel application runs smoothly.<\/p>\n<p>In Laravel 11, the process of setting up cron job task scheduling commands has changed.<\/p>\n<p>The Kernel.php file, traditionally used for this purpose, has been removed.<\/p>\n<p>Instead, cron jobs are now defined directly in the console.php file. Below, you\u2019ll find a straightforward example illustrating how to create a new command and configure it within Laravel 11<\/p>\n<p>So, let\u2019s see the laravel 11 scheduler command, laravel scheduler.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/debugspot.com\/blogs\/how-to-create-schedule-command-in-laravel-11\/#Step_1_Install_Laravel_11\" >Step 1: Install Laravel 11<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/debugspot.com\/blogs\/how-to-create-schedule-command-in-laravel-11\/#Step_2_Create_a_Custom_Command\" >Step 2: Create a Custom Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/debugspot.com\/blogs\/how-to-create-schedule-command-in-laravel-11\/#Step_3_Register_the_Task_Scheduler\" >Step 3: Register the Task Scheduler<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/debugspot.com\/blogs\/how-to-create-schedule-command-in-laravel-11\/#Step_4_Run_the_Scheduler_Command_Locally\" >Step 4: Run the Scheduler Command Locally<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/debugspot.com\/blogs\/how-to-create-schedule-command-in-laravel-11\/#Step_5_Set_Up_a_Cron_Job_on_the_Server\" >Step 5: Set Up a Cron Job on the Server<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/debugspot.com\/blogs\/how-to-create-schedule-command-in-laravel-11\/#Summary\" >Summary<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/debugspot.com\/blogs\/how-to-create-schedule-command-in-laravel-11\/#You_may_also_find_interesting\" >You may also find interesting:<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_1_Install_Laravel_11\"><\/span>Step 1: Install Laravel 11<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To start, you need a Laravel 11 project. Open your terminal and run the following command to install Laravel 11:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"> composer create-project laravel\/laravel example-app<\/pre>\n<p>This command sets up a new Laravel 11 project in a directory named example-app. Once the installation is complete, you can proceed to create your custom command.<\/p>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_2_Create_a_Custom_Command\"><\/span>Step 2: Create a Custom Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Laravel\u2019s Artisan CLI makes it easy to create custom commands. Use the following command to generate a new command class:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\">php artisan make:command TestCron --command=test:cron <\/code><\/pre>\n<p>This command creates a new command class named TestCron in the app\/Console\/Commands directory. Open the generated file TestCron.php and add the following code to define your command&#8217;s functionality:<\/p>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_3_Register_the_Task_Scheduler\"><\/span>Step 3: Register the Task Scheduler<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To ensure your custom command runs automatically, you need to schedule it. Open the routes\/console.php file and add the following code to register your command:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\">\r\nuse Illuminate\\Support\\Facades\\Artisan;\r\nuse Illuminate\\Support\\Facades\\Schedule;\r\n\r\nArtisan::command('test:cron')-&gt;describe('Run the test:cron command');\r\n\r\nSchedule::command('test:cron')-&gt;everyFiveMinutes();\r\n\r\n <\/code><\/pre>\n<p>This code schedules the test:cron command to run every five minutes.<\/p>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_4_Run_the_Scheduler_Command_Locally\"><\/span>Step 4: Run the Scheduler Command Locally<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To test your cron job locally, you can manually run the scheduler with the following command:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\"> php artisan schedule:run\r\n<\/code><\/pre>\n<p>If the command executes successfully, you should see log entries in the storage\/logs\/laravel.log file indicating that the cron job ran at the specified time:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\">\r\n[2024-04-10 23:45:03] local.INFO: Cron Job running at 2024-04-10 23:45:03\r\n[2024-04-10 23:50:05] local.INFO: Cron Job running at 2024-04-10 23:50:05\r\n[2024-04-10 23:55:04] local.INFO: Cron Job running at 2024-04-10 23:55:04\r\n<\/code><\/pre>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_5_Set_Up_a_Cron_Job_on_the_Server\"><\/span>Step 5: Set Up a Cron Job on the Server<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To automate your scheduled tasks on a server, set up a cron job. For an Ubuntu server, use the following command:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\"> crontab -e <\/code><\/pre>\n<p>Add the following line to the crontab file:<\/p>\n<pre class=\"highlight-height line-numbers language-javascript\"><code class=\"language-javascript\">* * * * * cd \/path-to-your-project &amp;&amp; php artisan schedule:run &gt;&gt; \/dev\/null 2&gt;&amp;1\r\n<\/code><\/pre>\n<p>This cron job will run the php artisan schedule:run command every minute. Replace \/path-to-your-project with the actual path to your Laravel project. Laravel will handle executing the appropriate scheduled tasks.<\/p>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>This cron job will run the php artisan schedule:run command every minute. Replace \/path-to-your-project with the actual path to your Laravel project. Laravel will handle executing the appropriate scheduled tasks.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"You_may_also_find_interesting\"><\/span>You may also find interesting:<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p><a title=\"Custom Middleware in Laravel 11\" href=\"https:\/\/debugspot.com\/blogs\/create-custom-middleware-in-laravel-11\/\" target=\"_blank\" rel=\"noopener\">Custom Middleware in Laravel 11<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to create and schedule custom commands in Laravel 11 with this detailed guide. This tutorial covers the installation of Laravel 11, creating a custom command, registering and scheduling it, and setting up a cron job on your server. Enhance your Laravel applications by automating tasks with cron jobs. Laravel 11 brings powerful features and improvements for managing background tasks and automating processes. This comprehensive guide will walk you through creating custom commands, scheduling them, and setting up cron jobs on your server to ensure your Laravel application runs smoothly. In Laravel 11, the process of setting up cron job task scheduling commands has changed. The Kernel.php file, traditionally used for this purpose, has been removed. Instead, cron jobs are now defined directly in the console.php file. Below, you\u2019ll find a straightforward example illustrating how to create a new command and configure it within Laravel 11 So, let\u2019s see the laravel 11 scheduler command, laravel scheduler. Step 1: Install Laravel 11 To start, you need a Laravel 11 project. Open your terminal and run the following command to install Laravel 11: composer create-project laravel\/laravel example-app This command sets up a new Laravel 11 project in a directory named example-app. &hellip;<\/p>\n","protected":false},"author":1,"featured_media":1915,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"rank_math_lock_modified_date":false,"footnotes":""},"categories":[3],"tags":[30],"class_list":["post-1506","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-laravel-11"],"acf":[],"_links":{"self":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1506"}],"collection":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/comments?post=1506"}],"version-history":[{"count":9,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1506\/revisions"}],"predecessor-version":[{"id":1916,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/posts\/1506\/revisions\/1916"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/media\/1915"}],"wp:attachment":[{"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/media?parent=1506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/categories?post=1506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debugspot.com\/blogs\/wp-json\/wp\/v2\/tags?post=1506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}