{"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<h3 class=\"wp-block-heading\">Step 1: Install Laravel 11<\/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\">Step 2: Create a Custom Command<\/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\">Step 3: Register the Task Scheduler<\/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\">Step 4: Run the Scheduler Command Locally<\/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\">Step 5: Set Up a Cron Job on the Server<\/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\">Summary<\/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>You may also find interesting:<\/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}]}}