Setting up Cron Jobs in Laravel

When I was asked to schedule some functionalities on one of my projects, I was thinking actually how that’s gonna be setup, and the real struggle was running a complex function on a daily or weekly schedule. But thanks to Laravel, it has made our lives so much easier.

So, in this tutorial, we’re gonna be learning how to schedule a DB operation in a Laravel system. (Cron Job)

Here are the things I have with me

  1. A Laravel Installation
  2. A Linux Server

Here, what we’re doing is, scheduling to delete a record from a database table. So, it may be some inactive record or expired item whatever, a record will be deleted everyday automatically.

Alright, let’s gets started!

First let’s create a custom command.

cd into the root of your Laravel installation and run the below command

php artisan make:command DeleteExpiredItem

What this does is, creating a custom command in Laravel that can be called with php artisan.

That comman will create new command file called DeleteExpiredItem.php in

app\Console\Commands

So, let’s open up that file and see what we can do with it


namespace App\Console\Commands;
class DeleteExpiredItem extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:name';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
}
}

Let’s add our custom command name and its description and finally the code we want to run when the command is called.

First, let’s add the name and description.


namespace App\Console\Commands;

use App\Photo_album;
use Illuminate\Console\Command;

class DeleteExpiredItem extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'deleteExpiredItem:delete';

/**
* The console command description.
*
* @var string
*/
protected $description = 'This command deletes expired items';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
}
}

 

Now it’s time to add our code we want to run when the command is called in the console.

That code goes inside handle() method

Here, in my case I want to delete an expired record from a table. I determine whether it is expired or not using some simple logic. Anyway, that’s unique to my case. This can be really differs from yours.

namespace App\Console\Commands;</code>

use App\Photo_album;
use Illuminate\Console\Command;

class DeleteExpiredItem extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'deleteExpiredItem:delete';

/**
* The console command description.
*
* @var string
*/
protected $description = 'This command deletes expired items';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$items = Item::all();

foreach ($items as $item) {
if( $item->expired == 1){
$item->delete();
}
}
}
}

 

Ok great! We’re done with this file.

Now it’s time to add this command to Laravel. For that, let’s open up

app\Console\Kernel.php

It will look like this…


namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [

];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')
->hourly();
}

/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}

Change this file as follows. Add the command file we just created to $commands array, and schedule it in the schedule() method.

namespace App\Console;</code>

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
'\App\Console\Commands\DeleteExpiredItem',
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('deleteExpiredItem:delete')
->daily();
}

/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
require base_path('routes/console.php');
}
}

Done! Now we have added our custom command to Laravel command list.

We can check this by typing…

php artisan list

This will list all the commands including our command we just created.

Running the Cron Job

Now you can run this command on console and see if it works as expected.

php artisan deleteExpiredItem:delete

You can check if the relevant items has been deleted after running this command. If it’s working, let’s move to the next step which is making this happen automatically.

For that, we need to edit web server’s crontab file

Login to your server and type this command with root access.

crontab -e

This will open up the crontab file of your server. Now add the following piece of code to it.

* * * * * php /path/to/artisan schedule:run &gt;&gt; /dev/null 2&gt;&amp;1

Save it and close.

That’s it folks! Now our the code will run on daily basis automatically.


Food for thought: PHP Imagick – create images with texts

Leave a Reply

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