美文网首页
定时任务

定时任务

作者: 小丑蛙很挑剔 | 来源:发表于2018-09-16 12:44 被阅读0次

实验环境

操作系统:Ubuntu 16.04.1 LTS

参考文章

https://www.cnblogs.com/lamp01/p/6864258.html

https://blog.csdn.net/a_new_steven/article/details/73733087

linux定时任务是由系统自带的crontab功能实现的,可以指定时间间隔或者特定命令的功能。与特定的编程语言和编程环境无关。

此次实验的是PHP laravel框架的定时任务实现

在laravel根目录下生成 cron.txt 文件,内容为

* * * * * php /home/bella/Downloads/lnmp/echo1.0/echo/artisan schedule:run >> /dev/null 2>&1

将文件路径传给crontab,crontab- l执行

crontab cron.txt

crontab -l

便会每分钟执行一次任务了

===================================================================================================

此次cron.txt中包含的PHP命令是定期执行laravel框架中定义的定时任务,所以我们要在laravel补充完整相关的任务定义。

laravel中的定时任务可以有很多种类型,这次我们使用命令的形式来定义定时任务。

在 App\Console\Commands\LogInfo 定义一个记录日志的方法。

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use Illuminate\Support\Facades\Log;

class LogInfo extends Command

{

    /**

    * The name and signature of the console command.

    *

    * @var string

    */

    protected $signature = 'lesson:log';

    /**

    * The console command description.

    *

    * @var string

    */

    protected $description = 'Log Info';

    /**

    * Create a new command instance.

    *

    * @return void

    */

    public function __construct()

    {

        parent::__construct();

    }

    /**

    * Execute the console command.

    *

    * @return mixed

    */

    public function handle()

    {

        Log::info('Crontab routing job. ');

    }

}

Laravel的计划任务调用是在 App\Console\Kernel 中的 schedule 方法中

在command方法中填入我们上面定义的'lesson:log'命令即可。

protected function schedule(Schedule $schedule)

    {

        // $schedule->command('inspire')

        //          ->hourly();

        $schedule->command('lesson:log')->everyMinute();

    }

如果 laravel目录下的 storage\logs\laravel.log 日志中每个1分钟会出现以下记录,即表示定时任务成功部署

[2018-09-15 10:30:01] local.INFO: Crontab routing job.

相关文章

  • 2019-07-31定时任务

    定时任务 定时任务实现方法 系统默认定时任务 用户自定义设置定时任务 定时任务配置文件 定时任务启动 定时任务样例...

  • 分布式定时调度-xxl-job

    一、定时任务概述 1.1 定时任务认识 1.1.1 什么是定时任务 定时任务是按照指定时间周期运行任务。使用场景为...

  • day 22 操作系统定时任务

    系统定时任务概念==生活中闹钟 系统定时任务实现方法: 实现定时任务配置: 定时任务如何进行设置 定时任务编写常见...

  • 7月30日 定时任务

    定时任务 代替人自动完成一些任务 定时任务实现的方法 定时任务软件:cronie定时任务软件:atd --- 设...

  • SpringBoot 定时任务

    1.如何定时任务 1.1 开启定时任务 1.2 @Scheduled(预定的)选择要定时执行的任务 == 定时在前...

  • crontab 定时任务

    查看当前用户的定时任务列表 创建(编辑)定时任务列表 定时任务格式 删除定时任务 注意 一定要设置crontab的...

  • 2019-10-14 定时任务方案

    定时任务方案 定时任务实现

  • Linux定时任务Crontab

    定时任务服务提供crontab命令来设定任务 定时任务命令: 定时任务服务提供crontab命令来设定任务 cro...

  • Android中 Handler延时 定时任务

    1.延时 2.定时任务,间隔固定时间执行某项任务 3.定时任务,间隔固定时间执行某项操作后关闭定时任务 参考:ht...

  • crondtab 定时任务

    编辑定时任务 crontab -e 查看定时任务 crontab -l 删除定时任务 crontab -r 如:*...

网友评论

      本文标题:定时任务

      本文链接:https://www.haomeiwen.com/subject/rwnxnftx.html