Wednesday, August 10, 2016

Add Cron jobs to run scheduling jobs for Laravel Developer on Ubuntu

I have faced a lot of problems to run schedule jobs in Laravel.

I am using Ubuntu 14.04 and i spent a lot of time to make it work.

Here i wrote down the basic procedure that i followed.

First 

To run scheduled jobs first you have to create a new command. To make a command go to your project directory and run following code in terminal:

 
php artisan make:console Hello

If you go to app/Console/Commands directory you'll see a new Hello.php class created.

Now change the signature variable and give a name of your newly created command

protected $signature = 'Hello';
Now change the description of the command if you like.

Now go to app/Console/kernal.php and update commands variable

protected $commands = [
    Hello::class,];
Don't forget to add namespace for Hello.php clas.
Now in handle function we want to do what ever we want. In this example we want to return 'Hi Rashed';

public function handle(){
Log::info('i was here ');}
and add use Log; at the of the Hello.php
Now Add following code in schedule function on kernal.php file.
protected function schedule(Schedule $schedule){    $schedule->command('Hello')->everyMinute();}
So you can see i am trying to run Hello command in every minutes.
Now Everything is ready and all we have to do is setting up a cron for this project.

To do so, open a terminal and type

corntab -e

Now add following line :

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
Now Run your Project and check storage/logs/laravel.log file to see message.

Related Posts:

  • Laravel Elixir Setup and Uses Laravel Elixir provides a clean, fluent API for defining basic Gulp tasks for your Laravel application. Elixir supports several common CSS and JavaScript pre-processors, and even testing tools. Using method chaining, … Read More
  • Install LAMP and PHPmyAdmin && PHPpgAdmin on Ubuntu 14.04 Install LAMP Steps are: sudo apt-get update sudo apt-get install apache2 sudo apt-get install mysql-server php5-mysql sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt sudo gedit /etc/apache2/conf-available/serve-… Read More
  • Laravel 5.2 Implicit model binding In Laravel 5.2 new feature called implicit model binding added. Which can be implement efficiently and in a very easy manner. Here how its done : In your routes.php file create a new route Route::get('user',function(){}); … Read More
  • Laravel Project Setup Using Composer In this tutorial i’ll write the key points that are necessary to create a laravel project/setting a laravel project with the help of composer. Composer Setup First We have to understand what is composer? Well, Composer is … Read More
  • Up & Running With Gulp Check Older version if installed $ npm rm --global gulp   Install Gulp Globally $ npm install --global gulp-cli Initializing Project directory npm init Installing Dependencies npm install --save-dev gulp Creatng gu… Read More

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. In latest Laravel version, there is option for command scheduling which you can use to create and schedule cron job in laravel (https://www.cloudways.com/blog/laravel-cron-job-scheduling/ ).

    ReplyDelete