Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cron Job with Laravel 4

I'm trying to find out how to set up a cron job in Laravel 4, and the command I would need to run in artisan for it.

In Laravel 3, there were Tasks but these don't seem to be there anymore and there is no documentation as to how to do it...

like image 976
TheSteed Avatar asked Jan 25 '13 11:01

TheSteed


3 Answers

Below I detail a tutorial for using commands in Laravel 4 with cron. I have divided into four steps to make it easier to follow.


STEP #1: Create a command in Laravel 4:

php artisan command:make RefreshStats

With command above, Laravel will create a file named RefreshStats.php in directory app/commands/


RefreshStats.php it's a file like this:

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class RefreshStats extends Command {

        protected $name = 'command:name';
        protected $description = 'Command description.';

        public function __construct() {
                parent::__construct();
        }

        public function fire(){

        }

        protected function getArguments() {
            return array(
                array('example', InputArgument::REQUIRED, 'An example argument.'),
            );
        }

        protected function getOptions() {
            return array(
                array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
            );
        }

}



STEP #2: Simple "configuration" of RefreshStats file:

You should change this line:

protected $name = 'command:name';

to something like this:

protected $name = 'refresh:stats';

If you don't need arguments (same for options), change this line:

protected function getArguments() {
      return array(
          array('example', InputArgument::REQUIRED, 'An example argument.'),
      );
}

to:

protected function getArguments() {
      return array();
}

And now pay attention to the fire function. The command will execute source code that is wrote in that function. Example:

public function fire(){
    echo "Hello world";    
}



STEP #3: Register the command:

You need to register the command. So open app/start/artisan.php file, and add one line as below:

Artisan::add(new RefreshStats);



STEP #4: Create CRON scheduled task:

Finally, you can add a scheduled task as follow:

crontab -e

And add a line (run command every 30 minutes) like below:

*/30 * * * * php path_laravel_project/artisan refresh:stats



And all will work automagically!

like image 62
tomloprod Avatar answered Nov 14 '22 03:11

tomloprod


Tasks have been replaced with commands, which are the same thing in Laravel 4, but integrated with Symfony's console component, and even more powerful than before.

like image 41
Dan Matthews Avatar answered Nov 14 '22 02:11

Dan Matthews


Alternative, if you don't like commands there is an unofficial Laravel 4 cron package: https://github.com/liebig/cron

like image 4
Marc Avatar answered Nov 14 '22 02:11

Marc