# 任务调度篇

TIP

laravel手册有详细的介绍,这里只是基于laravel做的快速使用示例.

Laravel 的命令行调度器允许在 Laravel 中清晰明了地定义命令调度。在使用这个任务调度器时,只需要在服务器上创建单个 Cron 入口。任务调度在 app/Console/Kernel.php 的 schedule 方法中进行定义.

# 前提

TIP

laravel12的调度任务已经放置到routes/console.php和bootstrap/app.php中

# 启动调度器

  • path-to-your-project项目部署的绝对路径
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
1

# 示例:

项目目录:

/www/wwwroot/youhujun.com/api.youhujun.com
1

执行

crontab -e
1

将如下内容添加进去

* * * * * cd /www/wwwroot/youhujun.com/api.youhujun.com && php artisan schedule:run >> /dev/null 2>&1
1

# 配置数据库备份

组件包发布成功以后在\cron\mysql_bal.sh.example预留了mysql数据库备份脚本

cp mysql_bal.sh.example mysql_bal.sh
1

根据自己的项目配置自行调整,配置好以后,自行调试代码

# /bin/sh
time=$(date "+%Y%m%d%H%M%S")
mysqldump -u用户名 -p密码% 数据库名 >  路径/数据库名$time.sql
1
2
3

# 调试统计命令

组件包已经预置了App\Console\Commands\LaravelFastApi\V1\ExecuteTotalCommand执行统计的命令

内容如下:

<?php

namespace App\Console\Commands\LaravelFastApi\V1;

use Illuminate\Console\Command;

use App\Facade\Common\V1\Total\TotalAllDataFacade;

class ExecuteTotalCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'execute:total';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'implement data statistics';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        //执行所有统计
        TotalAllDataFacade::doAllTotal();
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

可以看出App\Facade\Common\V1\Total\TotalAllDataFacade是一个统计门面, 也就是说实际后续业务中有其他的计划任务执行统计需要,都可以封装相应统计门面在这里执行

Last Updated: 12/31/2025, 6:59:40 AM