简体   繁体   English

Laravel 5.2如何在linux上的cron / php命令行上运行路由?

[英]Laravel 5.2 how to run route on cron / php command line on linux?

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. 尽管通过关于命令的文档(是如何在laravel上的命令行上运行php?)我根本就没有得到它。

For example I can run php script on command line on linux: 例如,我可以在linux上的命令行上运行php脚本:

php /path/to/my/phpfile.php

How on earth I can manage to do this on laravel? 我怎么能在laravel上做到这一点? Let's say I have a route to 假设我有一条路线

Route::get('/runthis', array('as' => 'runthis', 'uses' => 'Controller@runthis'));

How to run this on cron? 如何在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: 我会利用内核的schedule方法,只需添加* * * * * php /path/to/artisan schedule:run它可能看起来像这样:

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. 现在只需在crontab添加相应的条目,就可以了。

Updated with Cron Information 更新了Cron信息

cron tasks (AFAIK) don't support the seconds granularity. cron任务(AFAIK)不支持seconds粒度。 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. 相反,您需要在之前的2秒后执行30 times cron任务才能实现“每2秒”的cron作业。 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: 这是一个小图 ,我发现它非常有用于解释asterisk含义:

 * * * * *  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作为快捷方式:

->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

You wouldn't typically run a controller function in a cron. 您通常不会在cron中运行控制器功能。 You'd put the runthis logic in an Artisan command , then schedule it to run with Laravel's scheduler. 你将runthis逻辑放在Artisan命令中 ,然后安排它与Laravel的调度程序一起运行

如果你使用cron安排,你可以使用curl "url"来调用路由

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM