This is the way i've setup CRON jobs using Laravel 4 and the Artisan Command function.
Firstly, create a new command using Artisan. From the command line type:
php artisan command:make FooCommand
In your app/commands
folder you will now have a new file called FooCommand.php
.
Open that file up and write your code in the function fire()
. This will run every time your command runs. There are some other functions that allow you to capture arguments and options from the command line.
In your command file there are also $name
and $description
variables that need to be filled in. Give your task a nice name and description like:
/**
* The console command name.
*
* @var string
*/
protected $name = 'command:my_foo_command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'A description of what the command does';
Once you've finished you need to register it to Artisan by opening up app/start/artisan.php
and adding:
Artisan::add(new FooCommand);
Then using Artisan in the command line you can run your task using:
php artisan command:my_foo_command
This will only invoke the command once - to get it running on a regular basis add the following to your CRONTAB:
1 * * * * /usr/bin/php /path/to/the/artisan command:my_foo_command