If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.

Schedule Custom Artisan Command
Laravel एक powerful PHP framework है जो आपको काफी features provide करता है। इन features में से एक है Task Scheduling. ये feature आपको automate tasks को schedule करने कि सुविधा देता है, जैसे कि database cleanup, email notifications, backup generation, etc.
Laravel में tasks को automate करने के लिए आप Custom Artisan Commands create कर सकते हैं और उन्हें schedule भी कर सकते हैं।
इस blog में हम step-by-step सीखेंगे कैसे आप अपने custom commands को Laravel में schedule कर सकते हैं। चलिए शुरू करते हैं!
●●●
Laravel में अपने custom commands बनाने के लिए सबसे पहले हमें एक Artisan command create करनी होती है। Custom command बनाने के लिए, Laravel का artisan command-line tool use करते हैं।
php artisan make:command CustomTask
इससे app/Console/Commands/CustomTask.php file create हो जायेगी। आप इस file को अपने custom logic को लिखने के लिए use करेंगे।
CustomTask.php file के अंदर handle() method होता है, जहाँ पर आप अपना custom code लिखते हैं जो command run करते वक्त execute होता है।
namespace App\Console\Commands;
use Illuminate\Console\Command;
class CustomTask extends Command
{
// Command ka name jo CLI par dikhega
protected $signature = 'custom:task';
// Command ka description
protected $description = 'This command performs a custom task';
public function handle()
{
// Yahan aap jo task automate karna chahte hain wo likh sakte hain
\Log::info("Custom task executed successfully!");
// Agar kisi process ka output chahiye ho, toh use bhi display kar sakte hain
$this->info('Custom task has been successfully executed!');
}
}Explanation
protected $signature : यह command का name define करता है जो terminal में run करते वक्त use होता है (e.g., php artisan custom:task).
protected $description : यह एक short description है जो command के purpose को describe करता है।
handle() method में आप वो logic लिखते हैं जो command execute hone पर run होगा।
अब आपको अपने custom command को Laravel के command kernel में register करना पड़ेगा , ताकि Laravel उससे recognize करे।
इसके लिए आपको app/Console/Kernel.php file में जाना होगा और commands property में अपने custom command को add करें।
protected $commands = [
Commands\CustomTask::class,
];●●●
Laravel में tasks को schedule करना काफी simple है, और यह app/Console/Kernel.php file के अंदर किया जाता है।
Kernel.php में आपको schedule() method के अंदर अपनी command को schedule करना होगा।
use App\Console\Commands\CustomTask;
protected function schedule(Schedule $schedule)
{
$schedule->command(CustomTask::class)->daily();
}Explanation:
command() method का use करके हम अपने custom command को schedule करते हैं।
.daily() method का मतलब है कि यह command हर din एक बार execute होगी।
Laravel में कई तरह के scheduling frequencies हैं, जिन्हे आप official website पर देख सकते हैं।
जब आपने अपनी command को schedule कर लिया हो, तो अब आपको cron job set करना पड़ेगा जो scheduled tasks को run करे।
आप Laravel के scheduling को cron job के through automate कर सकते हैं। Cron job को set करने के लिए, terminal में यह command run करें।
crontab -e
इस command के बाद, cron file open हो जाएगी। उसमें यह line add करें।
* * * * * php /path/to/your/project/artisan schedule:run >> /dev/null 2>&1
इस cron job कि help से, Laravel हर minute check करेगा कि कोई scheduled task run करने का time है या नहीं। अगर है, तो वो task execute हो जायेगा।
ये configurations Linux OS के लिए हैं because 90% से ज्यादा servers Linux OS ही होते हैं , हालाँकि Window OS के लिए आपको अलग से CRON app install करना पड़ सकता हैं।
●●●
आप अपने custom command को manually भी test कर सकते हैं, यह देखने के लिए कि सब कुछ सही से काम कर रहा है.
Terminal में यह command run करें -
php artisan custom:task
अगर आपको expected output (e.g., log file में message) मिलता है, तो command सही से काम कर रही है।
आप manually schedule:run command को run करके भी देख सकते हैं।
php artisan schedule:run
schedule:run हमेशा background में cron job run नै करती command line tool close होते ही cron भी close हो जायगी हालाँकि testing के लिए ये सही है।
●●●
Laravel का scheduling feature काफी powerful है और आपको अपने custom commands को schedule करने में help करता है। आप अपनी daily, weekly, या monthly tasks को automate कर सकते हैं, जिससे कि आपका workflow काफी efficient हो जाता है।
इस blog में हमने step-by-step देखा कैसे अपने custom commands को create और उन्हें schedule करते हैं ।
Hope आपके लिए ये blog helpful रहा होगा :)
●●●
Loading ...