Laravel5.2如何在linux上的cron/php命令行上运行路由?

Laravel5.2如何在linux上的cron/php命令行上运行路由?,php,laravel,Php,Laravel,尽管阅读了关于命令的文档(这就是在laravel上的命令行上运行php的方式吗?),我还是一点也不懂 例如,我可以在linux上的命令行上运行php脚本: php /path/to/my/phpfile.php 我究竟怎样才能在拉威尔身上做到这一点?比如说我有一条去 Route::get('/runthis', array('as' => 'runthis', 'uses' => 'Controller@runthis')); 如何在cron上运行此功能?如果您使用cron进行调

尽管阅读了关于命令的文档(这就是在laravel上的命令行上运行php的方式吗?),我还是一点也不懂

例如,我可以在linux上的命令行上运行php脚本:

php /path/to/my/phpfile.php
我究竟怎样才能在拉威尔身上做到这一点?比如说我有一条去

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

如何在cron上运行此功能?

如果您使用cron进行调度,您只需使用
curl“url”
调用路由

通常不会在cron中运行控制器函数。你应该把
运行这个
逻辑放进去,然后使用Laravel的调度程序。

我个人会采取不同的方法。我会利用内核中的
调度
方法,简单地添加
***php/path/to/artisan schedule:run
,它可能看起来像这样:

class Kernel extends ConsoleKernel {
    protected $commands = [

    ];

    /* ... */
    protected function schedule(Schedule $schedule){
        $schedule->call(function(){
            //call your logic here
        })->cron('* * * * *');
    }
}
在:

此文件的结构如下所示:

class Kernel extends ConsoleKernel {
    protected $commands = [

    ];

    /* ... */
    protected function schedule(Schedule $schedule){
        $schedule->call(function(){
            //call your logic here
        })->cron('* * * * *');
    }
}
现在只需在
crontab
中添加适当的条目,就可以开始了

使用Cron信息更新

cron
任务(AFAIK)不支持
seconds
粒度。相反,为了实现“每2秒”cron作业,您需要在上一个cron任务之后执行30次cron任务。我知道没有其他方法可以达到这个目的

下面是一个我发现在解释星号的含义时非常有用的例子:

 * * * * *  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)
下面是您可以使用的选项列表,而不是
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

可能的复制品实际上是的,那会有用:)。我想可能会有一种“拉雷维尔之路”,而且似乎康曼德是一条路要走,但我认为我也很擅长这一点。谢谢是 啊我想是的,这是命令。我读了那个关于命令的文档,它并没有真正帮助我前进,但我又读了一遍,thanks@EmJeiEn医生很直截了当,你有什么问题?基本上,您可以执行类似于php artisan make:console RunThis
,修改生成的
app/console/Commands/RunThis.php
handle()
函数来实现所需的功能,然后执行类似于
$schedule->command('RunThis')->daily()的操作。cron('**')中的star()是什么意思;请解释一下,我需要每两秒钟运行一次cron。@Mandy为您更新了这篇文章,希望对您有所帮助。谢谢@Ohgodwhy,但我必须每两秒钟安排一次cron作业。而且它不是自动运行的,我在命令提示符下运行过它,在命令提示符下只运行过一次。请给我建议解决办法。