Laravel 定时任务 任务调度 可手动执行

1,776次阅读
没有评论

1、创建一个命令

php artisan make:command TestCommand 

执行成功后会提示:

Console command created successfully.

生成了一个新的命令文件

App\Console\Commands\TestCommand.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class TestCommand extends Command
{
    /**
     * The name and signature of the console command.
     * 命令名称(执行时需要用到)
     * @var string
     */
    protected $signature = 'test';

    /**
     * The console command description.
     * 命令描述
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * 处理业务逻辑
     * @return int
     */
    public function handle()
    {
        echo 123123;
        echo PHP_EOL;
        exit;
    }
}

2、配置console的Kernel

<?php

namespace App\Console;

use App\Console\Commands\TestCommand;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

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

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

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

3、执行命令

手动执行:

php artisan test(命令名称)

自动执行:

php artisan schedule:run

定时执行:crontab添加

php artisan schedule:run 

->cron(‘* * * * *’);
自定义 Cron 计划执行任务

->everyMinute();
每分钟执行一次任务

->everyFiveMinutes();
每五分钟执行一次任务

->everyTenMinutes();
每十分钟执行一次任务

->everyFifteenMinutes();
每十五分钟执行一次任务

->everyThirtyMinutes();
每三十分钟执行一次任务

->hourly();
每小时执行一次任务

->hourlyAt(17);
每小时第 17 分钟执行一次任务

->daily();
每天 0 点执行一次任务

->dailyAt(’13:00′);
每天 13 点执行一次任务

->twiceDaily(1, 13);
每天 1 点及 13 点各执行一次任务

->weekly();
每周日 0 点执行一次任务

->weeklyOn(1, ‘8:00’);
每周一的 8 点执行一次任务

->monthly();
每月第一天 0 点执行一次任务

->monthlyOn(4, ’15:00′);
每月 4 号的 15 点 执行一次任务

->quarterly();
每季度第一天 0 点执行一次任务

->yearly();
每年第一天 0 点执行一次任务

->timezone(‘America/New_York’);
设置时区

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2024 年 5 月
 12345
6789101112
13141516171819
20212223242526
2728293031  
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026 Jay 2025-12-22 09...
反超Nano Banana!OpenAI旗舰图像生成模型上线

反超Nano Banana!OpenAI旗舰图像生成模型上线

反超Nano Banana!OpenAI旗舰图像生成模型上线 Jay 2025-12-17 10:25:43 ...
“昆山杯”第二十七届清华大学创业大赛决赛举行

“昆山杯”第二十七届清华大学创业大赛决赛举行

“昆山杯”第二十七届清华大学创业大赛决赛举行 一水 2025-12-22 17:04:24 来源:量子位 本届...
企业级智能体落地,谁没踩这四种大坑?无问芯穹的系统性解法来了

企业级智能体落地,谁没踩这四种大坑?无问芯穹的系统性解法来了

企业级智能体落地,谁没踩这四种大坑?无问芯穹的系统性解法来了 衡宇 2025-12-16 20:10:53 来...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
小米语音首席科学家:AI发展的本质就像生物进化,不开源要慢1000倍 | MEET2026

小米语音首席科学家:AI发展的本质就像生物进化,不开源要慢1000倍 | MEET2026

小米语音首席科学家:AI发展的本质就像生物进化,不开源要慢1000倍 | MEET2026 克雷西 2025-...
顶尖技术+标准产品+创新模式+可靠服务,打造大模型商业落地中国范式

顶尖技术+标准产品+创新模式+可靠服务,打造大模型商业落地中国范式

顶尖技术+标准产品+创新模式+可靠服务,打造大模型商业落地中国范式 思邈 2025-12-16 10:24:0...
PPIO姚欣:AI正在进入自主行动与创造时代,智能体需要全新的操作系统|MEET2026

PPIO姚欣:AI正在进入自主行动与创造时代,智能体需要全新的操作系统|MEET2026

PPIO姚欣:AI正在进入自主行动与创造时代,智能体需要全新的操作系统|MEET2026 梦瑶 2025-12...
Dexmal原力灵机提出ManiAgent,用多智能体协作重构机器人操控

Dexmal原力灵机提出ManiAgent,用多智能体协作重构机器人操控

Dexmal原力灵机提出ManiAgent,用多智能体协作重构机器人操控 量子位的朋友们 2025-12-16...
推特吵架吵出篇论文!谢赛宁团队新作iREPA只要3行代码

推特吵架吵出篇论文!谢赛宁团队新作iREPA只要3行代码

推特吵架吵出篇论文!谢赛宁团队新作iREPA只要3行代码 henry 2025-12-16 15:03:31 ...