PHP lumen/laravel 定时任务 schedule crontab

41 篇文章 0 订阅
4 篇文章 0 订阅

* 查看php artisan可用的命令

/opt/lampp/bin/php /home/ubuntu/easyeye/src/admin/artisan help

* 添加crontab任务

crontab -e

 

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
 

打开后添加这一行

* * * * * /opt/lampp/bin/php /home/ubuntu/easyeye/src/admin/artisan schedule:run >> /dev/null 2>&1

注意最后面要添加一个空行

crontab: installing new crontab
new crontab file is missing newline before EOF, can't install.
Do you want to retry the same edit? (y/n) y
crontab: installing new crontab

 

列出当前的计划任务

$ crontab -l
* * * * * /opt/lampp/bin/php /home/ubuntu/easyeye/src/admin/artisan help schedule:run >> /dev/null 2>&1

 

* 定义artisan命令

E:\easyeye\src\admin\app\Console\Commands\TestCommand.php

<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class TestCommand extends Command {
    /**
     * @var string
     * The console command name.
     * */
    protected $name = 'test:putcache';

    /**
     * @var string
     * The console command description.
     */
    protected $description = 'test controller';

    /**
     * Execute the console command.
     * @return mixed
     */
    public function handle() {
        $now = new \DateTime('now');
        Log::info("schedule task at " . $now->format('Y-m-d H:i:s'));
    }
}

** list artisan commands

/opt/lampp/bin/php /home/ubuntu/easyeye/src/admin/artisan list

** Run the scheduled commands

/opt/lampp/bin/php /home/ubuntu/easyeye/src/admin/artisan schedule:run

** 测试新增的artisan命令是否可用

 /opt/lampp/bin/php /home/ubuntu/easyeye/src/admin/artisan test:putcache

$ tail /storage/logs/lumen-`date +%F`.log  

# 这里lumen log配置路径 

https://blog.csdn.net/fareast_mzh/article/details/82799647

默认是在 ./storage/logs/lumen.log

...
[2018-09-25 11:02:32] local.INFO: schedule task at 2018-09-25 11:02:32  

说明test:putcache这个artisan命令已经设置成功

 

* 添加lumen调度任务

./app/Console/Kernel.php

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel {
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \App\Console\Commands\TestCommand::class
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule) {
        $schedule->command('test:putcache')->everyMinute();
    }
}

 

添加计划任务还可以使用闭包

$schedule->call(function () {
    Log::info('任务调度');
})->everyMinute();

调度的时间可以有多种: 
->cron(‘* * * * *’); 在自定义Cron调度上运行任务 
->everyMinute(); 每分钟运行一次任务 
->everyFiveMinutes(); 每五分钟运行一次任务 
->everyTenMinutes(); 每十分钟运行一次任务 
->everyThirtyMinutes(); 每三十分钟运行一次任务 
->hourly(); 每小时运行一次任务 
->daily(); 每天凌晨零点运行任务 
->dailyAt(‘13:00’); 每天13:00运行任务 
->twiceDaily(1, 13); 每天1:00 & 13:00运行任务 
->weekly(); 每周运行一次任务 
->monthly(); 每月运行一次任务 
还有一下额外的方法,请参考:http://laravelacademy.org/post/235.html

 

* 执行计划任务

$ /opt/lampp/bin/php /home/ubuntu/easyeye/src/admin/artisan schedule:run
Running scheduled command: '/opt/lampp/bin/php-7.2.8' artisan test:putcache > '/dev/null' 2>&1

 

Reference:

laravel task schedule

https://laravel.com/docs/5.6/scheduling

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fareast_mzh

打赏个金币

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值