Laravel 5每30秒安排一次作业 [英] Laravel 5 schedule job every 30 seconds

查看:296
本文介绍了Laravel 5每30秒安排一次作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel时间表来运行cron作业.

I am using Laravel schedule to run cron jobs.

在我的crontab中,我已经添加了(具有正确的项目路径):

In my crontab I have added (with correct path to my project):

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

现在我的App \ Console \ Kernel.php看起来像:

Now my App\Console\Kernel.php looks like:

protected function schedule(Schedule $schedule)
{
        $schedule->command('investments:calculate_interests')->everyMinute();
        $schedule->call('\App\Http\Controllers\UsersController@testEvent')->everyMinute();
}

像这样,我每分钟都能成功运行作业.但是如何更改它以使其每10或20或30秒运行一次?

Using it like that I successfully run jobs every minute. But how I can change it to run them on every 10 or 20 or 30 seconds?

推荐答案

我使用以下代码解决了该问题:

I solved it with this code:

public function handle()
{
    $count = 0;
    while ($count < 59) {
        $startTime =  Carbon::now();

        $this->travelsRequestsDriversRepository->beFreeRequestAfterTime();

        $endTime = Carbon::now();
        $totalDuration = $endTime->diffInSeconds($startTime);
        if($totalDuration > 0) {
            $count +=  $totalDuration;
        }
        else {
            $count++;
        }
        sleep(1);
    }


}

并在cron表中添加此命令.

and in cron table add command of this.

* * * * * /usr/local/php56/bin/php56 /home/hamshahr/domains/hamshahrapp.com/project/artisan taxis:beFreeTaxis 1>> /dev/null 2>&1

这篇关于Laravel 5每30秒安排一次作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆