Despite going through documentation about commands (is that how you can run php on command line on laravel?) I just don't get it at all.
For example I can run php script on command line on linux:
php /path/to/my/phpfile.php
How on earth I can manage to do this on laravel? Let's say I have a route to
Route::get('/runthis', array('as' => 'runthis', 'uses' => 'Controller@runthis'));
How to run this on cron?
I would actually take a different approach to this personally. I would leverage the schedule
method off of the Kernel and simply add * * * * * php /path/to/artisan schedule:run
It may look something like this:
In:
App
|- Console
|- Kernel.php
The structure of this file would look something like this:
class Kernel extends ConsoleKernel {
protected $commands = [
];
/* ... */
protected function schedule(Schedule $schedule){
$schedule->call(function(){
//call your logic here
})->cron('* * * * *');
}
}
Now just add the appropriate entry to your crontab
and you're good to go.
Updated with Cron Information
cron
tasks (AFAIK) don't support the seconds
granularity. Instead, you would need to execute the cron task 30 times
2 seconds after the previous in order to achieve the "every 2 seconds" cron job. There's no other way that I know of to achieve this.
Here is a little diagram that I've found extremely useful in explaining what the asterisk
mean:
* * * * * command to execute
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ │
│ │ │ │ └───── day of week (0 - 7) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
│ │ │ └────────── month (1 - 12)
│ │ └─────────────── day of month (1 - 31)
│ └──────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)
And here's a list of your options you can use instead of cron
as shortcuts:
->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 13:00
->twiceDaily(1, 13); Run the task daily at 1:00 & 13:00
->weekly(); Run the task every week
->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
if you are scheduling with cron you can just use curl "url"
to call the route
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With