As per my experience in before we had to setup several configurations before setup a cronjob in Laravel.
In pervious settings we had to add each scheduler jobs into the Kernal.php file. But in Laravel 12 there is no Kernal.php files comes as a default.
So we can define schedulers inside the routes/console.php file. Let's take a look at how we can create a cronjob.
As a first step we need to create a custom command. For that we should run a command.
php artisan make:command SendReport
Next we can add our logic to the command file. Inside this command file we can use custom signature and description. This signature use to invoke this command.
This is a custom logic for an example. I used this signature for invoke this command.
Then you can define the scheduler inside the console.php file. Here I configured to run scheduler at each minute.
Now the configuration from the Laravel side is completed. Now we can define the cronjob inside the Linux server.
Open your one of system terminal to run these cronjob configurations. First you need to run the below command to add the cronjob.
crontab -e
After run command you can see a editor with already added cronjobs or empty page. So you need to add a cronjob settings specific to your project here.
As a first step you can check whether you have installed php using this command.
which php
if you already have php, then you can see a installed path. if not it is better to install php first.
Then you can add these cronjob configurations to the editor opened. This configuration relates for every minute.
* * * * * /usr/bin/env bash -lc "cd /you/project/location/my-app && /php/installed/location/php artisan schedule:run >> /you/project/location/storage/logs/cron.log 2>&1"
save this command and you can verify the cronjob using below command. for write a cronjob you can see more details from their website. CronJob
crontab -l
Now you can check this scheduler is working in the background. At anytime you can remove the cronjob using remove command.
crontab -r
These are all the settings for the Laravel scheduler jobs.
Thank you all have a nice day...........
Comments
Post a Comment