{"flag":true,"single":true,"pageTitle":"Task Scheduling in laravel 10, Make your own cronjob and run it","post":{"id":31,"user_id":"1","slug":"task-scheduling-in-laravel-10-make-your-own-cronjob-and-run-it-rarz","title":"Task Scheduling in laravel 10, Make your own cronjob and run it","body":"<p>Why this is important:<\/p>\r\n<ul>\r\n<li>schduled tasks<\/li>\r\n<li>In your controll<\/li>\r\n<li>view history, moniter etc<\/li>\r\n<\/ul>\r\n<p>Laravel actually allows two different ways to define scheduled tasks.&nbsp;<\/p>\r\n<p>1.<\/p>\r\n<p>Your task schedule is defined in the <strong>app\/Console\/Kernel.php<\/strong> file's schedule method<\/p>\r\n<p>In Kernel.php add follwing code it will execute every minute , also run command to make it working<\/p>\r\n<pre class=\"language-markup\"><code> protected function schedule(Schedule $schedule)\r\n{\r\n        $schedule-&gt;call(function () {\r\n            file_put_contents('public\/cron_sync_emails.log', file_get_contents(\"https:\/\/abc.site\/sync_emails\")); \/\/or any logic here\r\n        })-&gt;everyMinute();\r\n}<\/code><\/pre>\r\n<p>Run the command&nbsp;<\/p>\r\n<pre class=\"language-markup\"><code>php artisan schedule:work \/\/on local<\/code><\/pre>\r\n<p>On server SET THE CRON EVERY MINUTE<\/p>\r\n<pre class=\"language-markup\"><code>cd \/path-to-your-project &amp;&amp; php artisan schedule:run &gt;&gt; \/dev\/null 2&gt;&amp;1\r\nOR\r\ncd \/home\/boosxdf\/abc.site\/ &amp;&amp; \/usr\/local\/bin\/php artisan schedule:run &gt;\/dev\/null 2&gt;&amp;1  (WORKING ON NAMECHEAP)\r\nOR\r\ncd \/home\/boosxdf\/abc.site\/ &amp;&amp; \/usr\/bin\/php artisan schedule:run &gt;\/dev\/null 2&gt;&amp;1<\/code><\/pre>\r\n<p><strong>cd \/path-to-your-project:<\/strong> Project directory<\/p>\r\n<p><strong>&amp;&amp;:<\/strong> This is a command separator that allows us to chain commands together.<\/p>\r\n<p><strong>php artisan schedule:run:<\/strong> This command runs the Laravel scheduler, which executes any scheduled tasks that are due to run.<\/p>\r\n<p><strong>&gt;&gt; \/dev\/null: <\/strong>This redirects the output of the command to \/dev\/null, which discards it.<\/p>\r\n<p><strong>2&gt;&amp;1:<\/strong> This redirects the standard error output to the same place as the standard output.<\/p>\r\n<p>&nbsp;<\/p>\r\n<p><strong>OPEN URL IN Cron job<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>curl \"https:\/\/abc.site\/cron_test\" \/\/working<\/code><\/pre>\r\n<p>&nbsp;<\/p>\r\n<p><strong>DETAILED:<\/strong><\/p>\r\n<p><strong>Overview of your scheduled tasks<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>php artisan schedule:list<\/code><\/pre>\r\n<pre class=\"language-markup\"><code>\/\/ Run once per week on Monday at 1 PM...\r\n$schedule-&gt;call(function () {\r\n    \/\/\r\n})-&gt;weekly()-&gt;mondays()-&gt;at('13:00');\r\n \r\n\/\/ Run hourly from 8 AM to 5 PM on weekdays...\r\n$schedule-&gt;command('foo')\r\n          -&gt;weekdays()\r\n          -&gt;hourly()\r\n          -&gt;timezone('America\/Chicago')\r\n          -&gt;between('8:00', '17:00');<\/code><\/pre>\r\n<p>2.&nbsp;<strong>routes\/console.php <\/strong>Laravel also allows scheduling inside this file.<\/p>\r\n<pre class=\"language-markup\"><code>use Illuminate\\Support\\Facades\\Schedule;\r\nSchedule::command('emails:send')-&gt;daily();<\/code><\/pre>\r\n<p><strong>=======================<\/strong><br><strong>How to check if commands works<\/strong><br><strong>=======================<br>1.First Confirm the Commands Exist<br><\/strong><\/p>\r\n<pre class=\"language-markup\"><code>php artisan schedule:list<\/code><\/pre>\r\n<p>if output shows<br>Command &nbsp; &nbsp;Schedule<br>charity:send-reminders &nbsp; &nbsp;daily at 08:00<\/p>\r\n<p><strong>2. Test the Command Manually<br><\/strong><\/p>\r\n<pre class=\"language-markup\"><code>php artisan charity:send-reminders<\/code><\/pre>\r\n<p>If the command works, it will:&nbsp;&bull; execute normally&nbsp;&bull; show output&nbsp;&bull; or perform its task (email, sync etc)<\/p>\r\n<p><strong>3. Test the Scheduler Manually<br><\/strong><\/p>\r\n<pre class=\"language-markup\"><code>php artisan schedule:run<\/code><\/pre>\r\n<p>If nothing is scheduled for the current minute you will see something like: <strong>No scheduled commands are ready to run.<br>4. Force a Task to Run for Testing<br><\/strong><\/p>\r\n<p>Change temporarily to: <strong>-&gt;everyMinute();<br><\/strong>and again test using <strong>php artisan schedule:run<\/strong><\/p>\r\n<p><strong>5. Cron Setup With 5 Minute Limit because of namecheap limit<br><\/strong><\/p>\r\n<pre class=\"language-markup\"><code>cd \/home\/boosxdf\/abc.site\/ &amp;&amp; \/usr\/local\/bin\/php artisan schedule:run &gt;\/dev\/null 2&gt;&amp;1<\/code><\/pre>\r\n<p>&nbsp;<\/p>\r\n<p><strong>What If Cron Runs at 08:02?<\/strong><\/p>\r\n<p>If the cron was configured like this:&nbsp;*\/5 * * <em> <\/em>&nbsp;every five minutes at namecheap<\/p>\r\n<p>and started at 08:02, the execution times might be:<\/p>\r\n<p>08:02<br>08:07<br>08:12<\/p>\r\n<p>In that situation, Laravel would miss the exact 08:00 window, and the task would not run that day.<\/p>\r\n<p>However, most cron systems start cleanly at:&nbsp;00, 05, 10, 15, 20...<\/p>\r\n<p><strong>Example Timeline:<\/strong> $schedule-&gt;command('charity:send-reminders')-&gt;<strong><span style=\"color: rgb(45, 194, 107);\">dailyAt('08:00');<\/span> WORKS<br><\/strong><strong>Example Timeline:<\/strong> $schedule-&gt;command('charity:send-reminders')-&gt;<strong><span style=\"color: rgb(224, 62, 45);\">dailyAt('08:00'); NOT <\/span>WORKS<br><\/strong><br><strong>Time &nbsp; &nbsp;Cron Runs &nbsp; &nbsp;Task Runs<\/strong><br>07:55 &nbsp; &nbsp;yes &nbsp; &nbsp;no<br>08:00 &nbsp; &nbsp;yes &nbsp; &nbsp;YES<br>08:05 &nbsp; &nbsp;yes &nbsp; &nbsp;no<br>08:10 &nbsp; &nbsp;yes &nbsp; &nbsp;no<\/p>\r\n<p>&nbsp;<\/p>\r\n<p><strong>Scheduling Shell Commands<\/strong><\/p>\r\n<p>The exec method may be used to issue a command to the operating system:<\/p>\r\n<pre class=\"language-markup\"><code>use Illuminate\\Support\\Facades\\Schedule;\r\nSchedule::exec('node \/home\/forge\/script.js')-&gt;daily();<\/code><\/pre>\r\n<pre class=\"language-markup\"><code>Schedule::exec('mysqldump dbname &gt; backup.sql')-&gt;daily();<\/code><\/pre>\r\n<pre class=\"language-markup\"><code>Schedule::exec('python3 \/scripts\/analyze.py')-&gt;daily();<\/code><\/pre>\r\n<p><strong>Preventing Task Overlaps<\/strong><br>By default, scheduled tasks will be run even if the previous instance of the task is still running. To prevent this, you may use the withoutOverlapping method:<\/p>\r\n<pre class=\"language-markup\"><code>use Illuminate\\Support\\Facades\\Schedule;\r\nSchedule::command('emails:send')-&gt;withoutOverlapping();<\/code><\/pre>\r\n<p>the emails:send Artisan command will be run every minute if it is not already running. &nbsp;By default, the lock will expire after 24 hours:<\/p>\r\n<p><strong>Task Output:<\/strong> The Laravel scheduler provides several convenient methods for working with the output generated by scheduled tasks. First, using the sendOutputTo method, you may send the output to a file for later inspection:<\/p>\r\n<pre class=\"language-markup\"><code>use Illuminate\\Support\\Facades\\Schedule;\r\nSchedule::command('emails:send')\r\n    -&gt;daily()\r\n    -&gt;sendOutputTo($filePath); \/\/or     -&gt;appendOutputTo($filePath);<\/code><\/pre>\r\n<pre class=\"language-markup\"><code>Schedule::command('report:generate')\r\n    -&gt;daily()\r\n    -&gt;sendOutputTo($filePath)\r\n    -&gt;emailOutputTo('taylor@example.com');<\/code><\/pre>\r\n<p>&nbsp;<\/p>\r\n<p><strong>&nbsp;URL CALL USING SCHDULER:<br><\/strong><\/p>\r\n<pre class=\"language-markup\"><code>$schedule-&gt;call(function () {\r\n     $url = config('app.url') . '\/api\/coin-prices\/sync';\r\n     \\Illuminate\\Support\\Facades\\Http::get($url);\r\n})-&gt;everyFiveMinutes()-&gt;withoutOverlapping();<\/code><\/pre>\r\n<p><strong>&nbsp;<\/strong><\/p>\r\n<p><a href=\"https:\/\/laravel.com\/docs\/8.x\/scheduling\">https:\/\/laravel.com\/docs\/8.x\/scheduling<\/a><br><br><br><\/p>","category_id":"2","is_private":"0","created_at":"2023-03-20T00:41:29.000000Z","updated_at":"2026-03-25T06:43:22.000000Z","category":{"id":2,"user_id":"1","name":"Laravel Core","slug":"laravel-nhyt","parent_id":"1","created_at":"2023-03-14T03:58:27.000000Z","updated_at":"2023-03-20T11:30:50.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"Why this is important:  schduled tasks In your controll view history, moniter etc  Laravel actually allows two different ways to define sche - Task Scheduling in laravel 10, Make your own cronjob and run it (Updated: March 25, 2026) - Read more about Task Scheduling in laravel 10, Make your own cronjob and run it at my programming site [SITE]","categories":[]}