Laravel 5.5 - Task Scheduling With Cron Job Example

Today, we are share in this tutorials "How to set scron job in laravel 5.5 using scheduler for every minute." in many time you need to this type functionality in your laravel application. for Ex. any specific task or job execute any specific given time.

You can done this type functionality using laravel task scheduling. and some configure in your live server. in this post we are give very simple example for laravel cron job from the scratch with example.

In this tutorials we are make one simple cron job which change users table name change with some randome string in every one minute.

Simmply follow this step for set scron job in laravel using task scheduling.

Info Message!!
#Task Scheduling Cron Job Only Work On Live Server.

Yes, it is work on live server. but how to test in local we are also menson in this tutorils. once you test in local and it work as per your requirement then no worry.

Step : 1 Create New Command Class

First, we are need to create command class run by following comand in terminal.


php artisan make:console CronJob

After run this command then CronJob.php file automatic create in app/Console/Commands/ folder. so open it and simply place into it following code.


namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'User Name Change Successfully';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        \DB::table('users')
            ->where('id', 1)
            ->update(['name' => str_random(10)]);

    	$this->info('User Name Change Successfully!');
    }
}

Step : 2 Change In App\Console\Kernel.php

Next, open your App\Console\Kernel.php file and make following changes.


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 = [
         '\App\Console\Commands\CronJob',
    ];

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

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

Step : 3 Test Cron Job In Local

If you want to see your command description in terminal then run following command :


php artisan list
	

Now you can run your command to change users table's user name which id = 1.


php artisan CronJob:cronjob

After hit bellow comman in project root directory thhen your will show following output in terminal


User Name Change Successfully

Step : 4 Configure In Live Server

Now, last and final test to how to configur this cron job in live server. if you use linux shared server then login your server by ssh by this command


ssh username@ip_address

Then enter your server password

Now first check your php version in your server. by this command php -v

Now, run following command for set cron job in liver


crontab -e

After run this command then open one file in terminal go to the bottom and set your scron job like that for any time.

NOTE : For Ex. your php version is 7.1 in your live server and your laravel project host in this directory /var/www/html. then you should be set like that valye in open cron file.


* * * * * /usr/bin/php7.1 /var/www/html/artisan schedule:run 1>> /dev/null 2>&1
	
Schedule Options available in Laravel :

->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 midnight

->twiceDaily(1, 13); - Run the task daily at 1:00 & 13:00

->weekly(); - Run the task weekly

->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 Addiitional schedule constraints are listed below,

->weekdays(); - Limit the task to weekdays

->sundays(); - Limit the task to Sunday

->mondays(); - Limit the task to Monday

->tuesdays(); - Limit the task to Tuesday

->wednesdays(); - Limit the task to Wednesday

->thursdays(); - Limit the task to Thursday

->fridays(); - Limit the task to Friday

->saturdays(); - Limit the task to Saturday

->when(Closure); - Limit the task based on a truth test

If you want to any problem then please write comment and also suggest for new topic for make tutorials in future. Thanks...

 Prev post

Next post