How to run a cron job in laravel hosted in linux server
what is a cron job?
cron is a Linux utility which schedules a command or script on your server to run automatically at a specified time and date. A cron job is the scheduled task itself. Cron jobs can be very useful to automate repetitive tasks.
For example : In a website there are users who didn’t complete certain task or transaction we can send email or trigger some function as per our requirement. This has to be done at a particular time interval. Here comes the use of Cron jobs.
Creating Command in Laravel
Go to the folder which contains the laravel installed in it through terminal and type
You will get a screen like this.
You can see a command named make:console we are going to use that for creating our cron. All we need to run is
php artisan make:console CronExample --command=sendEmail:sample
Once this command is run you will get following
The newly above command will generate a php file named CronExample which can be found inside app/Console/Commands
In that file there will be two protected variable as $signature which by default will be filled by the command needed to run the this file through terminal. Another variable name will be $description which will be empty by default. We need to comment there what this command will do. For example : ‘send email to users who left without checking out’.
In that file you will find a function named handle(). Write your logic inside this function which will be executed each time the command is run (manually or through cron).
Then we need to go to app/Console/Kernel.php there inside class kernel there will be an protected variable with the name of $commands there we need to set the path as follows.
protected $commands = [ 'App\Console\Commands\CronExample' ];
Now go to terminal and type php artisan serve there you can find the command you created and the description.
So now your command is all set to run. You can test the command you created by running
php artisan sendEmail:sample
and it will run the functionality that’s written inside the handle function.
Now we need to schedule the function to run in particular interval of time. So all we need to do is set the time inside schedule function inside kernel.php
Or we can follow any other method as per our requirement.
||Run the task on a custom Cron schedule|
||Run the task every minute|
||Run the task every five minutes|
||Run the task every ten minutes|
||Run the task every thirty minutes|
||Run the task every hour|
||Run the task every day at midnight|
||Run the task every day at 13:00|
||Run the task daily at 1:00 & 13:00|
||Run the task every week|
||Run the task every month|
||Run the task every month on the 4th at 15:00|
||Run the task every quarter|
||Run the task every year|
||Set the timezone|
Let’s start the Laravel Scheduler
In order to check the scheduler we need to type the following command
We need to just run the following in our server to run the cron job
* * * * * php artisan schedule:run >> /dev/null 2>&1
The work of /dev/null 2>&1 is solely to discard the output that we get by running the above command.
Sometimes it is necessary to run a website for internal purpose within organisation or you might be want to share […]
By Murugan D
Principles to adhere to while designing mobile app’s In mobile app development, the UI (user interface) and UX (user experience) […]
By Punith B M