How to create and run multiple cron Jobs in Laravel?

| By Webner

Here is the process for creating and runnning multiple cron Jobs in Laravel

Cron: It is a scheduler which runs after a specific interval or at specific time. It should be used when developer wants to run a piece of code at a particular time or interval.

Laravel provides a feature to handle cron jobs in a simple way without hustling with the OS level configurations. We can create multiple cron jobs in laravel itself. It just needs one entry, the cron list, to run the created crons in the laravel. Below is the procedure to create cron job for a specific task in laravel.

1. Firstly you need to run a command as below which will create a file in “app/Console/Commands/” folder with a <CommandName> name. All commands will be created in the “Commands” folder.

php artisan make:command CommandName

2. Now go to the created file. There will be two variables:
$signature-used to set the name for that command like “exampleName”. Using this $signature value, we will be able to run the new created command.
$description – you can set $description variable according to your functionality like “example command code..” This variable is for description of command. This will be shown when we will list the all commands using below command:

php artisan route:list

3. In command file, there will be a function with the name “handle”. This function will run whenever we will run this command. For example paste below code in which we are sending mail.

$to = ''; 
$headers = "From: ".$to."\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$subject ="Request"; $message = "demo";
mail($to, $subject, $message, $headers);

4. Then save it. You can test this command by running below command:

php artisan signatureName

5. To schedule this command, go to “app/Console/Kernel.php”. There will be a command array defined in the file. Now replace the command array as given below.

protected $commands = [

6. There will be a function with the name “schedule” in the Kernal.php. Replace it with the below code.

protected function schedule(Schedule $schedule)

In above code, $schedule->command(‘signatureName’) tells that which command needs to be run.

hourly() tells that command should run after every hour.
withoutOverlapping() function will not allow to run command again if it is already running. If you want to run the command with overlapping then you can remove it.

Below are functions to define time interval in the schedule.

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
->everyFifteenMinutes(); Run the task every fifteen minutes
->everyThirtyMinutes(); Run the task every thirty minutes
->hourly(); Run the task every hour
->hourlyAt(17); Run the task every hour at 17 mins past the 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

7. All the laravel configuration is completed. Now you just need to do an entry in the cron manager of your server as given below.

8. * * * * * php /project-location/artisan schedule:run << /dev/null 2>&1

schedule:run – handles the execution of all commands defined in the Kernel.php.

9. Now cron will run hourly and mail will be sent.

Leave a Reply

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