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
OK so I found this useful for setting up crons in laravel 4.2.
Alternative, if you don't like commands there is an unofficial Laravel 4 cron package: https://github.com/liebig/cron
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.
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.
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),
);
}
}
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";
}
You need to register the command. So open app/start/artisan.php
file, and add one line as below:
Artisan::add(new RefreshStats);
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