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

php artisan

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

For example

$schedule->command('sendEmail:sample')->hourly();

Or we can follow any other method as per our requirement.

Method Description
->cron('* * * * * *'); Run the task on a custom Cron schedule
->everyMinute(); Run the task every minute
->everyFiveMinutes(); Run the task every five minutes
->everyTenMinutes(); Run the task every ten minutes
->everyThirtyMinutes(); Run the task every thirty minutes
->hourly(); Run the task every hour
->daily(); Run the task every day at midnight
->dailyAt('13:00'); Run the task every day at 13:00
->twiceDaily(1, 13); Run the task daily at 1:00 & 13:00
->weekly(); Run the task every week
->monthly(); Run the task every month
->monthlyOn(4, '15:00'); Run the task every month on the 4th at 15:00
->quarterly(); Run the task every quarter
->yearly(); Run the task every year
->timezone('America/New_York'); Set the timezone

Let’s start the Laravel Scheduler

In order to check the scheduler we need to type the following command

crontab -e

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.

Leave a Reply

Your email address will not be published. Required fields are marked *