5

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?

EmJeiEn
  • 1,343
  • 5
  • 17
  • 30

3 Answers3

3

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
Community
  • 1
  • 1
Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • What is the meaning by star(*) in cron('* * * * *'); Please explain, i need to run cron on every two second. – Mandy Jul 15 '16 at 07:34
  • @Mandy Big update to the post for you, I hope you find it helpful. – Ohgodwhy Jul 15 '16 at 20:46
  • Thanks @Ohgodwhy, but i have to schedule cron job for every 2 second. and it is not running automatically, I have run it from command prompt and it runs only one time from command prompt. Please suggest me the solution. – Mandy Jul 18 '16 at 08:54
2

if you are scheduling with cron you can just use curl "url" to call the route

David Nguyen
  • 8,368
  • 2
  • 33
  • 49
  • Actually yeah, that would work :). Thought could there be a "laravel way" though and it seems comands are the way to go, but I'm good with this as well I think. Thanks! – EmJeiEn Mar 09 '16 at 18:06
1

You wouldn't typically run a controller function in a cron. You'd put the runthis logic in an Artisan command, then schedule it to run with Laravel's scheduler.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • Yeah.. Thought so it's with commands. I read that doc about commands and it didn't really help me forward but I read it again, thanks – EmJeiEn Mar 09 '16 at 18:09
  • @EmJeiEn Docs are pretty straightforward, what are you having trouble with? You'd essentially do something like `php artisan make:console RunThis`, modify the resulting `app/Console/Commands/RunThis.php` `handle()` function to do the functionality you want, and then something like `$schedule->command('runthis')->daily();` in your scheduler. – ceejayoz Mar 09 '16 at 18:15